リワード広告は、ユーザーが広告を操作することと引き換えにアプリ内報酬を提供する広告です。このガイドでは、 Ad Managerのリワード広告を iOS アプリに組み込む方法を説明します。
Prerequisites
- Google Mobile Ads SDK 8.0.0 以降
- スタートガイドの手順を完了していること
常にテスト広告でテストする
アプリの開発とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。
テスト広告を読み込む際は、次に示す iOS リワード広告向けのテスト専用広告ユニット ID を使うと簡単です。
/6499/example/rewarded
この ID は、すべてのリクエストに対してテスト広告を返す特別な ID で、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、このテスト用 ID は、アプリを公開する前に必ずご自身の広告ユニット ID に置き換えてください。
Mobile Ads SDK のテスト広告の仕組みについて詳しくは、テスト広告をご覧ください。
実装
リワード インタースティシャル広告を組み込む際の基本的な流れは次のとおりです。
- 広告を読み込む
- (省略可)SSV コールバックを検証する
- コールバックを登録する
- 広告を表示してリワード イベントを処理する
広告を読み込む
広告の読み込みは、GADRewardedAd
クラスの静的 loadWithAdUnitID:request:completionHandler:
メソッドを使用して行います。読み込みメソッドには、広告ユニット ID と GAMRequest
オブジェクトのほか、広告の読み込みの成功時または失敗時に呼び出される完了ハンドラが必要です。読み込まれた GADRewardedAd
オブジェクトは、完了ハンドラのパラメータとして提供されます。次の例は、ViewController
クラスで GADRewardedAd
を読み込む方法を示しています。
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { private var rewardedAd: GADRewardedAd? func loadRewardedAd() { let request = GAMRequest() GADRewardedAd.load(withAdUnitID:"/6499/example/rewarded", request: request, completionHandler: { [self] ad, error in if let error = error { print("Failed to load rewarded ad with error: \(error.localizedDescription)") return } rewardedAd = ad print("Rewarded ad loaded.") } ) } }
Objective-C
@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADRewardedAd *rewardedAd; @end @implementation ViewController - (void)loadRewardedAd { GAMRequest *request = [GAMRequest request]; [GADRewardedAd loadWithAdUnitID:@"/6499/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
クエリ パラメータに渡されます。カスタムデータ値が設定されていない場合、SSV コールバックは custom_data
クエリ パラメータ値を持ちません。
次のコードサンプルは、広告をリクエストする前にリワード広告オブジェクトにカスタムデータを設定する方法を示しています。
Swift
GADRewardedInterstitialAd.load(withAdUnitID:"/6499/example/rewarded", request: request, completionHandler: { [self] ad, error in if let error != error { rewardedInterstitialAd = ad let options = GADServerSideVerificationOptions() options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING" rewardedInterstitialAd.serverSideVerificationOptions = options }
Objective-C
GAMRequest *request = [GAMRequest request]; [GADRewardedInterstitialAd loadWithAdUnitID:@"/6499/example/rewarded" request:request completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { if (error) { // Handle Error return; } self.rewardedInterstitialAd = ad; GADServerSideVerificationOptions *options = [[GADServerSideVerificationOptions alloc] init]; options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING"; ad.serverSideVerificationOptions = options; }];
コールバックを登録する
プレゼンテーション イベントの通知を受け取るには、GADFullScreenContentDelegate
プロトコルを実装し、返された広告の fullScreenContentDelegate
プロパティに割り当てる必要があります。GADFullScreenContentDelegate
プロトコルは、広告の表示が成功または失敗した場合のコールバックと、広告が閉じられた場合のコールバックを処理します。次のコードは、プロトコルを実装して広告に割り当てる方法を示しています。
Swift
class ViewController: UIViewController, GADFullScreenContentDelegate { private var rewardedAd: GADRewardedAd? func loadRewardedAd() { let request = GAMRequest() GADRewarded.load(withAdUnitID:"/6499/example/rewarded", request: request, completionHandler: { [self] ad, error in if let error = error { print("Failed to load rewarded ad with error: \(error.localizedDescription)") return } rewardedAd = ad print("Rewarded ad loaded.") rewardedAd?.fullScreenContentDelegate = self } ) } /// 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() { if let ad = rewardedAd { ad.present(fromRootViewController: self) { let reward = ad.adReward print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)") // TODO: Reward the user. } } else { print("Ad wasn't ready") } }
Objective-C
- (void)show { ... if (self.rewardedAd) { [self.rewardedAd presentFromRootViewController:self 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
プロパティを参照すると、アダプタが広告リクエストの処理に対応できない理由を確認できます。userDidEarnRewardHandler
完了ハンドラは、常にadDidDismissFullScreenContent:
デリゲート メソッドの前に呼び出されますか?Google 広告では、すべての
userDidEarnRewardHandler
呼び出しはadDidDismissFullScreenContent:
の前に行われます。メディエーションを通じて配信される広告の場合、サードパーティの広告ネットワーク SDK の実装によってコールバックの順序が決まります。リワード情報を含む単一のデリゲート メソッドを提供する広告ネットワーク SDK の場合、メディエーション アダプタは、adDidDismissFullScreenContent:
の前にuserDidEarnRewardHandler
を呼び出します。
GitHub の例
- リワード広告の例: Swift | Objective-C
次のステップ
詳しくは、ユーザーのプライバシーについての記事をご覧ください。