使用者可選擇與獎勵廣告互動以換取應用程式內獎勵。本指南說明如何將 AdMob的獎勵廣告整合至 iOS 應用程式。 參閱客戶成功案例: 個案研究 1、個案研究 2。
先備知識
- Google Mobile Ads SDK 8.0.0 以上版本。
- 完成入門指南。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非即時的實際廣告。否則可能導致帳戶遭到停權。
要載入測試廣告,最簡單的方法就是使用 iOS 獎勵廣告專用的測試廣告單元 ID:
ca-app-pub-3940256099942544/1712485313
這項機制經過特別設定,可針對每個請求傳回測試廣告,您可以在編寫程式碼、進行測試及偵錯時,自由用於自家的應用程式。但別忘了在發布應用程式前,用自己的廣告單元 ID 取代這個 ID。
如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」一文。
導入作業
整合插頁式獎勵廣告的主要步驟如下:
- 載入廣告
- [選用] 驗證 SSV 回呼
- 註冊回呼
- 顯示廣告並處理獎勵事件
載入廣告
您可以在 GADRewardedAd
類別上使用靜態 loadWithAdUnitID:request:completionHandler:
方法載入廣告。載入方法需要廣告單元 ID、GADRequest
物件,以及廣告載入成功或失敗時呼叫的完成處理常式。系統會在完成處理常式中將載入的 GADRewardedAd
物件做為參數提供。以下範例說明如何在 ViewController
類別中載入 GADRewardedAd
。
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
private var rewardedAd: GADRewardedAd?
func loadRewardedAd() {
do {
rewardedAd = try await GADRewardedAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
} catch {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}
}
}
Objective-C
@import GoogleMobileAds;
@import UIKit;
@interface ViewController ()
@property(nonatomic, strong) GADRewardedAd *rewardedAd;
@end
@implementation ViewController
- (void)loadRewardedAd {
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
return;
}
self.rewardedAd = ad;
NSLog(@"Rewarded ad loaded.");
}];
}
[選用] 驗證伺服器端驗證 (SSV) 回呼
如果應用程式需要在伺服器端驗證回呼中使用額外資料,則應使用獎勵廣告的自訂資料功能。在獎勵廣告物件上設定的任何字串值都會傳遞至 SSV 回呼的 custom_data
查詢參數。如果未設定自訂資料值,custom_data
查詢參數便不會出現在 SSV 回呼中。
以下程式碼範例示範如何在要求廣告前,設定獎勵廣告物件的自訂資料。
Swift
do {
rewardedAd = try await GADRewardedAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
let options = GADServerSideVerificationOptions()
options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
rewardedAd.serverSideVerificationOptions = options
} catch {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}
Objective-C
[GADRewardedAd
loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
request:[GADRequest request];
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
// Handle Error
return;
}
self.rewardedAd = ad;
GADServerSideVerificationOptions *options =
[[GADServerSideVerificationOptions alloc] init];
options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
ad.serverSideVerificationOptions = options;
}];
註冊回呼
為了接收簡報事件的通知,您必須導入 GADFullScreenContentDelegate
通訊協定,並指派給傳回廣告的 fullScreenContentDelegate
屬性。GADFullScreenContentDelegate
通訊協定會處理廣告成功或失敗及關閉時的回呼。下列程式碼顯示如何導入通訊協定,並將其指派給廣告:
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController, GADFullScreenContentDelegate {
private var rewardedAd: GADRewardedAd?
func loadRewardedAd() {
do {
rewardedAd = try await GADRewardedAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
rewardedAd?.fullScreenContentDelegate = self
} catch {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}
}
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad will present full screen content.
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad will present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
}
Objective-C
@interface ViewController () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADRewardedAd *rewardedAd;
@end
@implementation ViewController
- (void)loadRewardedAd {
GADRequest *request = [GADRequest request];
[GADRewardedAd
loadWithAdUnitID:@"ca-app-pub-3940256099942544/4806952744"
request:request
completionHandler:^(GADRewardedAd *ad, NSError *error) {
if (error) {
NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
return;
}
self.rewardedAd = ad;
NSLog(@"Rewarded ad loaded.");
self.rewardedAd.fullScreenContentDelegate = self;
}];
}
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"Ad did fail to present full screen content.");
}
/// Tells the delegate that the ad will present full screen content.
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"Ad will present full screen content.");
}
/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"Ad did dismiss full screen content.");
}
GADRewardedAd
是一次性的物件。也就是說,獎勵廣告顯示後就無法再次顯示。最佳做法是在 GADFullScreenContentDelegate
的 adDidDismissFullScreenContent:
方法中載入其他獎勵廣告,這樣下一個獎勵廣告關閉後就能立即開始載入。
顯示廣告並處理獎勵事件
向使用者顯示獎勵廣告之前,您必須讓使用者有明確選擇觀看獎勵廣告內容,以換取獎勵。獎勵廣告必須一律可供使用者選擇觀看。
顯示廣告時,您必須提供 GADUserDidEarnRewardHandler
物件來處理使用者獎勵。
以下程式碼是顯示獎勵廣告的最佳方法。
Swift
func show() {
guard let rewardedAd = rewardedAd else {
return print("Ad wasn't ready.")
}
// The UIViewController parameter is an optional.
ad.present(fromRootViewController: nil) {
let reward = ad.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
// TODO: Reward the user.
}
}
Objective-C
- (void)show {
if (self.rewardedAd) {
// The UIViewController parameter is nullable.
[self.rewardedAd presentFromRootViewController:nil
userDidEarnRewardHandler:^{
GADAdReward *reward =
self.rewardedAd.adReward;
// TODO: Reward the user!
}];
} else {
NSLog(@"Ad wasn't ready");
}
}
常見問題
- 我可以取得
GADRewardedAd
的獎勵詳細資料嗎? - 是的,如果您在觸發
userDidEarnReward
回呼前需要有獎勵金額,GADRewardedAd
提供adReward
屬性,您可以在廣告載入後檢查用於驗證獎勵金額。 - 初始化呼叫是否有逾時?
- 10 秒後,Google Mobile Ads SDK 會叫用提供給
startWithCompletionHandler:
方法的GADInitializationCompletionHandler
,即使中介服務聯播網尚未完成初始化也一樣。 - 如果我收到初始化回呼時,有些中介服務聯播網還沒準備就緒,該怎麼辦?
建議您在
GADInitializationCompletionHandler
中載入廣告。即使中介服務聯播網尚未準備就緒,Google Mobile Ads SDK 仍會要求該聯播網提供廣告。因此,如果中介服務聯播網在逾時後完成初始化作業,仍可在該工作階段期間處理日後的廣告請求。您可以呼叫
GADMobileAds.initializationStatus
,繼續在應用程式工作階段中,輪詢所有轉接程式的初始化狀態。- 如何找出特定中介服務聯播網尚未準備就緒的原因?
GADAdapterStatus
物件的description
屬性說明轉接程式為何尚未準備好為廣告請求提供服務。- 是否一律會在
adDidDismissFullScreenContent:
委派方法之前呼叫userDidEarnRewardHandler
完成處理常式? 如果是 Google Ads,所有
userDidEarnRewardHandler
呼叫都會在adDidDismissFullScreenContent:
之前發生。對於透過中介服務放送的廣告,回呼順序取決於第三方廣告聯播網 SDK 的實作方式。如果廣告聯播網 SDK 提供單一委派方法並提供獎勵資訊,中介服務轉接程式會在adDidDismissFullScreenContent:
之前叫用userDidEarnRewardHandler
。
GitHub 上的範例
- 獎勵廣告範例:Swift | Objective-C
後續步驟
進一步瞭解使用者隱私。