全画面広告のベータ版 API に移行する

Google Mobile Ads SDK v7.67.0 は、一貫性向上の目的で汎用的な全画面表示の広告スタイルを共有するインタースティシャル広告とリワード広告のベータ版 API を導入しています。これらの API は GADInterstitialAdBetaGADRewardedAdBeta で表され、2021 年初頭に GADInterstitialGADRewardedAd にとって代わります。このガイドでは、インタースティシャル広告とリワード広告のベータ版への移行方法を説明します。

新しいベータ版 API のメリット

全画面広告の新しい API と現在の API には、主に次の 2 つの違いがあります。

  1. 静的クラスメソッド load

    全画面広告の読み込みと表示に関する現在の手法は次のとおりです。

    1. 広告オブジェクトのインスタンスを作成し、そのインスタンスへの参照を保持します。
    2. 読み込みコールバックと表示コールバックを処理するデリゲートを 1 つ割り当てます。
    3. 広告を読み込みます。
    4. isReady を使って、広告が読み込まれたかどうかを確認します。
    5. その広告を表示します。

    新しい API ではこの手法が少し変わり、読み込みコールバックがデリゲートに含まれなくなりました。代わりに、読み込みコールバックが完了ハンドラとして load メソッドに渡されます。

    全画面広告の読み込みと表示の新しい手法は次のとおりです。

    1. 広告クラスで静的読み込みメソッドを呼び出し、読み込み完了ハンドラを指定します。
    2. 読み込み完了コールバックで、返されて読み込んだ広告への参照を保持します。
    3. 表示コールバックを処理するデリゲートを割り当てます。
    4. その広告を表示します。

    新しい手法には次のメリットがあります。

    • 読み込まれていない広告への参照を保持することがありません。
    • 読み込み中の広告のオブジェクトを保持する必要がありません。
  2. 一貫した広告イベント。

    イベントの種類 既存の API ベータ版 API
    読み込みイベント GADInterstitialDelegate または GADRewardedAdDelegate GADInterstitialAdBetaLoadCompletionHandler または GADRewardedAdBetaLoadCompletionHandler
    表示イベント GADFullScreenContentDelegate

    現在、広告イベントをリッスンするには、GADInterstitialDelegate プロトコルを実装するクラスをインタースティシャルのデリゲート プロパティに登録するか、GADRewardedAdDelegate プロトコルを実装するクラスをリワード広告のデリゲート プロパティに登録する方法をとります(ご利用の広告フォーマットに応じて、いずれかを選択します)。以前はこの同じデリゲートに、広告の読み込みと表示の両方のライフサイクル関連メソッドがありました。

    新しい API では、読み込みイベントと表示イベントが別々になったため、広告を表示する前にいつでも GADFullScreenContentDelegate を登録できるようになり、広告を読み込む前に単体のデリゲートを設定する必要がなくなりました。各フォーマットに固有の広告読み込みイベントは、読み込みメソッドで渡される単体の読み込み完了ハンドラに移っています。

インタースティシャル

広告を読み込む

下記のコード スニペットは、インタースティシャル広告を読み込んで、広告の読み込みが成功または失敗したときのイベントをリッスンする方法を示しています。

現在の API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADInterstitialDelegate {

  var interstitial: GADInterstitial!

  override func viewDidLoad() {
    super.viewDidLoad()
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    interstitial.delegate = self
    let request = GADRequest()
    interstitial.load(request)
  }

  /// Tells the delegate an ad request succeeded.
  func interstitialDidReceiveAd(_ ad: GADInterstitial) {
    print("Interstitial ad loaded.")
  }

  /// Tells the delegate an ad request failed.
  func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
    print("Interstitial ad failed to load with error: \(error.localizedDescription)")
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[GADInterstitial alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
  self.interstitial.delegate = self;
  GADRequest *request = [GADRequest request];
  [self.interstitial loadRequest:request];
}

/// Tells the delegate an ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
  NSLog(@"Insterstitial ad loaded.");
}

/// Tells the delegate an ad request failed.
- (void)interstitial:(GADInterstitial *)ad
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"Interstitial ad failed to load with error: %@", [error localizedDescription]);
}

ベータ版 API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  var interstitial: GADInterstitialAdBeta?

  override func viewDidLoad() {
    super.viewDidLoad()
    let request = GADRequest()
    GADInterstitialAdBeta.load(withAdUnitID:"ca-app-pub-8123415297019784/4985798738",
                               request: request,
                               completionHandler: { (ad, error) in
                                if let error = error {
                                  print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                                  return
                                }
                                self.interstitial = ad
                                self.interstitial.fullScreenContentDelegate = self
    })
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADInterstitialAdBeta *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
                                  request:request
                        completionHandler:^(GADInterstitialAdBeta *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
    self.interstitial.fullScreenContentDelegate = self;
  }];
}

広告を表示する

現在の API

Swift

func showInterstitial() {
  ...
  if interstitial.isReady {
    interstitial.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showInterstitial: {
  ...
  if (self.interstitial.isReady) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

ベータ版 API

Swift

func showInterstitial() {
  ...
  if let ad = interstitial {
    ad.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showInterstitial: {
  ...
  if (self.interstitial) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

広告表示イベント

下記のコード スニペットは、広告表示が成功または失敗した場合と拒否された場合のコールバックの処理方法を示しています。

現在の API

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
  interstitial.delegate = self
  ...
}

/// Tells the delegate that an interstitial will be presented.
func interstitialWillPresentScreen(_ ad: GADInterstitial) {
  print("Interstitial ad will be presented.")
}

/// Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
  print("Interstitial ad will be dismissed.")
}

/// Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
  print("Interstitial ad dismissed.")
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
  print("Interstitial ad will leave application.")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:"ca-app-pub-3940256099942544/4411468910"];
  self.interstitial.delegate = self;
  ...
}

/// Tells the delegate that an interstitial will be presented.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will be presented.");
}

/// Tells the delegate the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will be dismissed.");
}

/// Tells the delegate the interstitial had been animated off the screen.
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad dismissed.");
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
  NSLog(@"Interstitial ad will leave application.");
}

ベータ版 API

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADInterstitialAdBeta.load(withAdUnitID:"ca-app-pub-8123415297019784/4985798738",
                             request: request,
                             completionHandler: { (ad, error) in
                              if let error = error {
                                print(error.localizedDescription)
                                return
                              }
                              self.interstitial = ad
                              self.interstitial.fullScreenContentDelegate = self
  })
}

func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did present full screen content.")
}

func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
  print("Ad failed to present full screen content with error \(error.localizedDescription).")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("Ad did dismiss full screen content.")
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
                                  request:request
                        completionHandler:^(GADInterstitialAdBeta *ad, NSError *error) {
    if (error) {
      NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription])
      return;
    }
    self.interstitial = ad;
    self.interstitial.fullScreenContentDelegate = self;
  }];
}

- (void)adDidPresentFullScreenContent:(id)ad {
  NSLog(@"Ad did present full screen content.");
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]);
}

- (void)adDidDismissFullScreenContent:(id)ad {
  NSLog(@"Ad did dismiss full screen content.");
}

リワード広告

広告を読み込む

現在の API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADRewardedAdDelegate {
  /// The rewarded ad.
  var rewardedAd: GADRewardedAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    rewardedAd = GADRewardedAd(adUnitID: "ca-app-pub-3940256099942544/1712485313")
    rewardedAd.delegate = self
    rewardedAd?.load(GADRequest()) { error in
      if let error = error {
        print("Rewarded ad failed to load with error: \(error.localizedDescription)")
      } else {
        print("Rewarded ad loaded.")
      }
    }
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADRewardedAd *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.rewardedAd = [[GADRewardedAd alloc]
      initWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"];
  self.rewardedAd.delegate = self;
  GADRequest *request = [GADRequest request];
  [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
    if (error) {
      NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]);
    } else {
      NSLog(@"Rewarded ad loaded.");
    }
  }];
}

ベータ版 API

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {
  /// The rewarded ad.
  var rewardedAd: GADRewardedAdBeta?

  override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADRewardedAdBeta.load(withAdUnitID: "ca-app-pub-8123415297019784/9501821136",
                         request: request, completionHandler: { (ad, error) in
                          if let error = error {
                            print("Rewarded ad failed to load with error: \(error.localizedDescription)")

                            return
                          }
                          self.rewardedAd = ad
                          self.rewardedAd?.fullScreenContentDelegate = self
    })
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () 

@property(nonatomic, strong) GADRewardedAdBeta *rewardedAd;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  GADRequest *request = [GADRequest request];
  [GADRewardedAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                              request:request
                    completionHandler:^(GADRewardedAdBeta *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;
}

広告を表示して特典を処理する

リワード広告では、ユーザーが特典を獲得したときにイベントを処理する必要があります。広告を表示するためには、GADRewardedAd API を使って GADRewardedAdDelegate プロトコルの一環として rewardedAd:userDidEarnReward: を実装し、GADRewardedAdBeta API を使って GADUserDidEarnRewardHandler API を実装します。

現在の API

Swift

func showRewardedAd() {
  ...
  if rewardedAd.isReady {
    rewardedAd.present(fromRootViewController: self delegate:self)
  } else {
    print("Ad wasn't ready")
  }
}

/// Tells the delegate that the user earned a reward.
func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarnReward: GADAdReward) {
  // TODO: Reward the user.
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

/// Tells the delegate that the user earned a reward.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward {
  // TODO: Reward the user.
}

ベータ版 API

Swift

func showRewardedAd() {
  ...
  if let ad = rewardedAd {
      ad.present(fromRootViewController: self,
                 userDidEarnRewardHandler: {
                  let reward = ad.adReward
                  // TODO: Reward the user.
      })
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd) {
    [self.rewardedAd presentFromRootViewController:self
                          userDidEarnRewardHandler:^ {
      GADAdReward *reward = self.rewardedAd.adReward;
      // TODO: Reward the user.
    }];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

広告表示イベント

GADRewardedAd API で、広告を表示するメソッドに GADRewardedAdDelegate を渡します。広告を表示する前に、GADRewardedAdBeta API でプロパティとして GADFullscreenContentDelegate を広告に設定します。

現在の API

Swift

func showRewardedAd() {
  ...
  if rewardedAd.isReady {
    rewardedAd.present(fromRootViewController: self delegate:self)
  } else {
    print("Ad wasn't ready")
  }
}

/// Tells the delegate that the rewarded ad was presented.
func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad presented.")
}
/// Tells the delegate that the rewarded ad was dismissed.
func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) {
  print("Rewarded ad dismissed.")
}
/// Tells the delegate that the rewarded ad failed to present.
func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) {
  print("Rewarded ad failed to present with error: \(error.localizedDescription).")
}

Objective-C

- (void)showRewardedAd: {
  ...
  if (self.rewardedAd.isReady) {
    [self.rewardedAd presentFromRootViewController:self delegate:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

/// Tells the delegate that the rewarded ad was presented.
- (void)rewardedAdDidPresent:(GADRewardedAd *)rewardedAd {
  NSLog(@"Rewarded ad presented.");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error {
  NSLog(@"Rewarded ad failed to present with error: %@",
        [error localizedDescription]);
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
  NSLog(@"Rewarded ad dismissed.");
}

ベータ版 API

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  let request = GADRequest()
  GADRewardedAdBeta.load(withAdUnitID: "ca-app-pub-8123415297019784/9501821136",
                         request: request, completionHandler: { (ad, error) in
                          if let error = error {
                            print(error.localizedDescription)
                            return
                          }
                          self.rewardedAd = ad
                          self.rewardedAd?.fullScreenContentDelegate = self
    })
  }

  /// Tells the delegate that the rewarded ad was presented.
  func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Rewarded ad presented.")
  }
  /// Tells the delegate that the rewarded ad was dismissed.
  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Rewarded ad dismissed.")
  }
  /// Tells the delegate that the rewarded ad failed to present.
  func ad(_ ad: GADFullScreenPresentingAd,
      didFailToPresentFullScreenContentWithError error: Error) {
    print("Rewarded ad failed to present with error: \(error.localizedDescription).")
  }

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  GADRequest *request = [GADRequest request];
  [GADRewardedAdBeta loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313"
                              request:request
                    completionHandler:^(GADRewardedAdBeta *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 rewarded ad was presented.
- (void)adDidPresentFullScreenContent:(id)ad {
  NSLog(@"Rewarded ad presented.");
}

/// Tells the delegate that the rewarded ad failed to present.
- (void)ad:(id)ad
    didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"Rewarded ad failed to present with error: %@",
       [error localizedDescription]);
}

/// Tells the delegate that the rewarded ad was dismissed.
- (void)adDidDismissFullScreenContent:(id)ad {
  NSLog(@"Rewarded ad dismissed.");
}