使用者可選擇與獎勵廣告互動 獲得應用程式內獎勵本指南 說明如何整合 Ad Manager 獎勵廣告 下載到 iOS 應用程式中
必要條件
- Google Mobile Ads SDK 8.0.0 以上版本。
- 完成入門指南。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非 現場及正式環境廣告否則可能導致帳戶遭到停權。
要載入測試廣告,最簡單的方法就是使用我們專屬的 iOS 測試廣告單元 ID 獎勵廣告:
/21775744923/example/rewarded
這項機制經過特別設定,可針對每個請求傳回測試廣告 在您的程式設計、測試和偵錯時,可以免費使用應用程式。只需製作 務必先用廣告單元 ID 取代廣告單元,再發布應用程式。
若要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試 Google Ads。
導入作業
整合獎勵廣告的主要步驟如下:
- 載入廣告
- [選用] 驗證 SSV 回呼
- 註冊回呼
- 顯示廣告並處理獎勵事件
載入廣告
系統是透過
上的 loadWithAdUnitID:request:completionHandler:
方法
GADRewardedAd
類別。載入方法需要廣告單元 ID、
GAMRequest
物件和完成處理常式
系統在廣告載入成功或失敗時呼叫載入
系統會在完成提示中提供 GADRewardedAd
物件做為參數
處理常式。以下範例說明如何在GADRewardedAd
您的 ViewController
類別。
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
private var rewardedAd: GADRewardedAd?
func loadRewardedAd() {
do {
rewardedAd = try await GADRewardedAd.load(
withAdUnitID: "/21775744923/example/rewarded", request: GAMRequest())
} 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 {
GAMRequest *request = [GAMRequest request];
[GADRewardedAd
loadWithAdUnitID:@"/21775744923/example/rewarded"
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: "/21775744923/example/rewarded", request: GAMRequest())
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:@"/21775744923/example/rewarded"
request:[GAMRequest 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: "/21775744923/example/rewarded", request: GAMRequest())
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 {
GAMRequest *request = [GAMRequest 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 會叫用
GADInitializationCompletionHandler
提供給startWithCompletionHandler:
方法,就算中介服務聯播網尚未 並完成初始化。 - 如果我收到初始化回呼時,有些中介服務聯播網還沒準備就緒,該怎麼辦?
建議您將廣告載入
GADInitializationCompletionHandler
。即使中介服務聯播網尚未準備就緒 Google Mobile Ads SDK 仍會要求該聯播網提供廣告。如果 中介服務聯播網在逾時後完成初始化作業,仍可運作 封鎖日後的廣告請求您可以持續輪詢所有轉接程式的初始化狀態 呼叫
GADMobileAds.initializationStatus
來修正應用程式工作階段。- 如何找出特定中介服務聯播網尚未準備就緒的原因?
GADAdapterStatus
物件的description
屬性會說明為何 轉接程式尚無法用於處理廣告請求。- 是否一律會在
adDidDismissFullScreenContent:
委派方法之前呼叫userDidEarnRewardHandler
完成處理常式? Google Ads 的所有
userDidEarnRewardHandler
呼叫都會發生adDidDismissFullScreenContent:
前。對於透過 中介服務,也就是第三方廣告聯播網 SDK 的實作方式會決定回呼順序。針對符合下列條件的廣告聯播網 SDK 提供獎勵資訊的單一委派方法,中介服務轉接程式 在adDidDismissFullScreenContent:
之前叫用userDidEarnRewardHandler
。
GitHub 上的範例
- 獎勵廣告範例: Swift | Objective-C
後續步驟
進一步瞭解使用者隱私。