リワード広告

プラットフォームを選択: Android iOS Unity Flutter

リワード広告は、ユーザーが広告を操作することと引き換えにアプリ内で報酬を獲得できる広告です。このガイドでは、AdMob のリワード広告を iOS アプリに組み込む方法を説明します。 次の成功事例もご覧ください。事例紹介 1事例紹介 2

前提条件

常にテスト広告でテストする

アプリの開発とテストでは必ずテスト広告を使用し、配信中の実際の広告は使用しないでください。実際の広告を使用すると、アカウントが停止される可能性があります。

テスト広告を読み込む際は、次に示す iOS リワード広告向けのテスト専用広告ユニット ID を使うと簡単です。

ca-app-pub-3940256099942544/1712485313

この ID は、すべてのリクエストに対してテスト広告を返すように構成されており、アプリのコーディング、テスト、デバッグで自由に使うことができます。なお、アプリを公開する前に、必ずテスト用 ID をご自身の広告ユニット ID に置き換えてください。

Mobile Ads SDK のテスト広告の仕組みについて詳しくは、テスト広告をご覧ください。

実装

リワード広告を組み込む際の基本的な流れは次のとおりです。

  • 広告を読み込む
  • [省略可] SSV コールバックを検証する
  • コールバックを登録する
  • 広告を表示してリワード イベントを処理する

広告を読み込む

広告の読み込みは、GADRewardedAd クラスで load(adUnitID:request) メソッドを使用して行います。

Swift

func loadRewardedAd() async {
  do {
    rewardedAd = try await RewardedAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "ca-app-pub-3940256099942544/1712485313", request: Request())
    rewardedAd?.fullScreenContentDelegate = self
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}

SwiftUI

import GoogleMobileAds

class RewardedViewModel: NSObject, ObservableObject, FullScreenContentDelegate {
  @Published var coins = 0
  private var rewardedAd: RewardedAd?

  func loadAd() async {
    do {
      rewardedAd = try await RewardedAd.load(
        with: "ca-app-pub-3940256099942544/1712485313", request: Request())
      rewardedAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

// Replace this ad unit ID with your own ad unit ID.
[GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
              request:[GADRequest request]
    completionHandler:^(GADRewardedAd *ad, NSError *error) {
      if (error) {
        NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
        return;
      }
      self.rewardedAd = ad;
      self.rewardedAd.fullScreenContentDelegate = self;
    }];

任意: サーバーサイド認証(SSV)コールバックを検証する

サーバーサイド認証コールバックで追加データを必要とするアプリでは、リワード広告のカスタムデータ機能を使用する必要があります。リワード広告オブジェクトに設定されている文字列値はすべて、SSV コールバックの custom_data クエリ パラメータに受け渡されます。カスタムデータ値が設定されていない場合、SSV コールバックは custom_data クエリ パラメータ値を持ちません。

次のコード例は、広告をリクエストする前に、リワード広告オブジェクトにカスタムデータを設定する方法を示したものです。

Swift

do {
  rewardedAd = try await RewardedAd.load(
    // Replace this ad unit ID with your own ad unit ID.
    with: "ca-app-pub-3940256099942544/1712485313", request: Request())
  let options = ServerSideVerificationOptions()
  options.customRewardText = "SAMPLE_CUSTOM_DATA_STRING"
  rewardedAd?.serverSideVerificationOptions = options
} catch {
  print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}

Objective-C

// Replace this ad unit ID with your own ad unit ID.
[GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                        request:[GADRequest request]
              completionHandler:^(GADRewardedAd *ad, NSError *error) {
                if (error) {
                  NSLog(@"Rewarded ad failed to load with error: %@", error.localizedDescription);
                  return;
                }
                self.rewardedAd = ad;
                GADServerSideVerificationOptions *options =
                    [[GADServerSideVerificationOptions alloc] init];
                options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
                ad.serverSideVerificationOptions = options;
              }];

コールバックを登録する

表示イベントに関する通知を受け取るには、返された広告の GADFullScreenContentDelegate to thefullScreenContentDelegate` プロパティを割り当てる必要があります。

Swift

rewardedAd?.fullScreenContentDelegate = self

SwiftUI

rewardedAd?.fullScreenContentDelegate = self

Objective-C

self.rewardedAd.fullScreenContentDelegate = self;

GADFullScreenContentDelegate プロトコルは、広告の表示が成功または失敗した場合のコールバックと、広告が閉じられた場合のコールバックを処理します。次のコードは、プロトコルを実装する方法を示しています。

Swift

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called.")
  // Clear the rewarded ad.
  rewardedAd = nil
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called with error: \(error.localizedDescription).")
}

SwiftUI

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the rewarded ad.
  rewardedAd = nil
}

Objective-C

- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the rewarded ad.
  self.rewardedAd = nil;
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}

広告を表示してリワード イベントを処理する

リワード広告をユーザーに表示する前に、リワード広告のコンテンツを報酬と引き換えに視聴するかどうか、明確な選択肢を示す必要があります。リワード広告は、必ずユーザーの許可を受けてから表示しなければなりません。

広告を表示する際には、ユーザーへの報酬を処理する GADUserDidEarnRewardHandler オブジェクトを指定する必要があります。

以下のコードは、リワード広告を表示するための最適な方法を示しています。

Swift

rewardedAd.present(from: self) {
  let reward = rewardedAd.adReward
  print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")

  // TODO: Reward the user.
}

SwiftUI

ビューの UI イベントをリッスンして、広告を表示するタイミングを判断します。

var body: some View {
  VStack(spacing: 20) {
      Button("Watch video for additional 10 coins") {
        viewModel.showAd()
        showWatchVideoButton = false
      }

ビューモデルからリワード広告を表示します。

func showAd() {
  guard let rewardedAd = rewardedAd else {
    return print("Ad wasn't ready.")
  }

  rewardedAd.present(from: nil) {
    let reward = rewardedAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}

Objective-C

[self.rewardedAd presentFromRootViewController:self
                      userDidEarnRewardHandler:^{
                        GADAdReward *reward = self.rewardedAd.adReward;
                        NSString *rewardMessage = [NSString
                            stringWithFormat:@"Reward received with currency %@ , amount %lf",
                                             reward.type, [reward.amount doubleValue]];
                        NSLog(@"%@", rewardMessage);

                        // TODO: Reward the user.
                      }];

よくある質問

GADRewardedAd の報酬の詳細情報を取得することはできますか?
はい。userDidEarnReward コールバックが呼び出される前に報酬値を確認する必要がある場合、GADRewardedAd では adReward プロパティを使って、広告の読み込み後に報酬値の情報を取得できます。
初期化の呼び出しでタイムアウトは発生しますか?
Google Mobile Ads SDK では、10 秒が経過すると、メディエーション ネットワークの初期化が完了していなくても、startWithCompletionHandler: メソッドで指定されている GADInitializationCompletionHandler が呼び出されます。
初期化コールバックを取得したときに、対応準備が完了していないメディエーション ネットワークはどうなりますか?

GADInitializationCompletionHandler 内で広告を読み込むことをおすすめします。メディエーション ネットワークの対応準備が完了していなくても、Google Mobile Ads SDK はそのネットワークに対して広告を要求します。そのため、いったんタイムアウトされても、その後に初期化が完了すれば、メディエーション ネットワークはそのセッション中に発生する後続の広告リクエストに対応することができます。

GADMobileAds.initializationStatus を呼び出せば、アプリ セッションの間、すべてのアダプタの初期化ステータスを継続的にポーリングすることができます。

特定のメディエーション ネットワークの対応準備が完了していない理由を確認するには、どうすればよいですか?

GADAdapterStatus オブジェクトの description プロパティを参照すると、アダプタが広告リクエストの処理に対応できない理由を確認できます。

userDidEarnRewardHandler 完了ハンドラは、常に adDidDismissFullScreenContent: デリゲート メソッドの前に呼び出されますか?

Google 広告では、すべての userDidEarnRewardHandler 呼び出しは adDidDismissFullScreenContent: の前に行われます。メディエーションを通じて配信される広告の場合、サードパーティの広告ネットワーク SDK の実装によってコールバックの順序が決まります。リワード情報を含む単一のデリゲート メソッドを提供する広告ネットワーク SDK の場合、メディエーション アダプタは、adDidDismissFullScreenContent: の前に userDidEarnRewardHandler を呼び出します。

GitHub の例

リワード広告の完全な例を、ご希望の言語でご覧ください。

次のステップ

ユーザーのプライバシーの詳細をご確認ください。