リワード インタースティシャル(ベータ版)

リワード インタースティシャルは、 報酬型広告フォーマットで、表示された広告に対して報酬を提供できる 自動的に最適化されます。リワード広告とは異なり、ユーザーは オプトイン インタースティシャルを表示する必要があります。

前提条件

実装

リワード インタースティシャル広告を実装する主な手順は次のとおりです。

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

広告を読み込む

広告の読み込みは、静的な 次に対する loadWithAdUnitID:request:completionHandler: メソッド: GADRewardedInterstitialAd クラス。読み込みメソッドでは、広告ユニット ID、 GAMRequest オブジェクト、完了ハンドラ これは、広告の読み込みが成功または失敗したときに呼び出されます。読み込まれた GADRewardedInterstitialAd オブジェクトは、完了時にパラメータとして提供されます。 使用します。次の例は、GADRewardedInterstitialAd を読み込む方法を示しています。 ViewController クラス。

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
    } catch {
      print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

Objective-C

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"<var label='the ad unit ID'>/21775744923/example/rewarded_interstitial</var>"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

[省略可] サーバー側の検証(SSV)コールバックを検証する

サーバーサイドで追加データを必要とするアプリ 確認コールバックでは、 リワード広告のカスタムデータ機能を使用します。リワード広告に設定されている文字列値 SSV コールバックの custom_data クエリ パラメータに渡されます。「いいえ」の場合 カスタムデータ値が設定されている場合、custom_data クエリ パラメータ値は SSV コールバックに含まれます。

次のコードサンプルは、リワード広告にカスタムデータを設定する方法を示しています。 広告のリクエスト前にインタースティシャル広告オブジェクトを表示します。

Swift

do {
  rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
    withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
  let options = GADServerSideVerificationOptions()
  options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
  rewardedInterstitialAd.serverSideVerificationOptions = options
} catch {
  print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}

Objective-C

[GADRewardedInterstitialAd
    loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
              request:GAMRequest
    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

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
      self.rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded interstitial ad 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) GADRewardedInterstitialAd *rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd,
          NSError *_Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
          self.rewardedInterstitialAd.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.");
}

広告を表示して報酬イベントを処理する

広告を表示する際は、GADUserDidEarnRewardHandler オブジェクトを指定する必要があります。 報酬を処理することにしました

次のコードは、リワード広告を表示するのに最適な方法を示しています。 インタースティシャル広告です。

Swift

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

  // The UIViewController parameter is an optional.
  rewardedInterstitialAd.present(fromRootViewController: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    // TODO: Reward the user.
  }
}

Objective-C

- (void)show {
  // The UIViewController parameter is nullable.
  [_rewardedInterstitialAd presentFromRootViewController:nil
                                userDidEarnRewardHandler:^{

                                  GADAdReward *reward =
                                      self.rewardedInterstitialAd.adReward;
                                  // TODO: Reward the user.
                                }];
}

次のステップ

詳しくは、ユーザーのプライバシーについての説明をご覧ください。