インタースティシャル広告

インタースティシャル広告は、ユーザーが閉じるまでアプリのインターフェース上に全画面表示される広告です。通常は、次のアクティビティに移行する前や、ゲームレベルをクリアした後の合間など、アプリの操作中に画面が切り替わるタイミングで表示されます。アプリにインタースティシャル広告が表示されると、ユーザーは広告をタップしてリンク先 URL に移動するか、広告を閉じてアプリに戻るかを選択することになります。

このガイドでは、インタースティシャル広告を iOS アプリに統合する方法について説明します。

Prerequisites

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

アプリを作成してテストする際は、実際の本番環境広告ではなく、テスト広告を使用してください。実際の広告を使用すると、アカウントが停止される可能性があります。

テスト広告を読み込む際は、次に示す iOS インタースティシャル向けのテスト専用広告ユニット ID を使うと簡単です。
/6499/example/interstitial

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

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

実装

インタースティシャル広告を組み込む主な手順は、以下のとおりです。

  1. 広告を読み込みます。
  2. コールバックを登録する
  3. 広告を表示してリワード イベントを処理する

広告を読み込む

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

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    let request = GAMRequest()
    GAMInterstitialAd.load(withAdManagerAdUnitID: "/6499/example/interstitial",
                                         request: request,
                               completionHandler: { [self] ad, error in
                        if let error = error {
                          print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                          return
                        }
                        interstitial = ad
                      }
    )
  }
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GAMInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GAMRequest *request = [GAMRequest request];
  [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/6499/example/interstitial"
                                       request:request
                             completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
  }];
}

コールバックを登録する

表示イベントに関する通知を受け取るには、GADFullScreenContentDelegate プロトコルを実装し、返された広告の fullScreenContentDelegate プロパティに割り当てる必要があります。GADFullScreenContentDelegate プロトコルは、広告の表示が成功または失敗した場合のコールバックと、広告が閉じられた場合のコールバックを処理します。以下のコードは、プロトコルを実装して広告に割り当てる方法を示しています。

Swift

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()
    let request = GAMRequest()
    GAMInterstitialAd.load(withAdManagerAdUnitID: "/6499/example/interstitial",
                                         request: request,
                               completionHandler: { [self] ad, error in
                        if let error = error {
                          print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                          return
                        }
                        interstitial = ad
                        interstitial?.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) GAMInterstitialAd *interstitial;

@end

@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  GAMRequest *request = [GAMRequest request];
  [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/6499/example/interstitial"
                                       request:request
                             completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
    self.interstitial.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.");
}

GAMInterstitialAd は使い捨てオブジェクトです。つまり、インタースティシャル広告を一度表示すると、再度表示することはできません。おすすめの方法は、GADFullScreenContentDelegateadDidDismissFullScreenContent: メソッドで別のインタースティシャル広告を読み込んでおくことです。これにより、前のインタースティシャル広告が閉じられ次第、次のインタースティシャル広告の読み込みを開始できます。

広告を表示する

インタースティシャル広告は、ゲームのレベルが切り替わる合間やタスクが完了した直後など、アプリの実行の流れが自然に一時停止するタイミングで表示される必要があります。UIViewController のアクション メソッドのいずれかでこれを行う例を次に示します。

Swift

@IBAction func doSomething(_ sender: Any) {
  if interstitial != nil {
    interstitial.present(fromRootViewController: self)
  } else {
    print("Ad wasn't ready")
  }
}

Objective-C

- (IBAction)doSomething:(id)sender {
  ...
  if (self.interstitial) {
    [self.interstitial presentFromRootViewController:self];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

広告が返されず、リクエスト エラー「リクエスト エラー: 表示する広告がありません」というエラーが表示される場合は、広告申込情報でターゲットに設定されているサイズが適切であることを確認します。インタースティシャルのサイズは、スマートフォンでは 320x480、480x320、タブレットでは 1024x768、768x1024 です。1024×768 や 768×1024 に収まるほどの画面サイズがない場合、320×480 または 480×320 のサイズにフォールバックします。

おすすめの方法

インタースティシャル広告が自分のアプリに適しているかどうかをよく見極めましょう。
インタースティシャル広告は、画面の切り替わりがあるアプリに適しています。画像の共有やゲームレベルのクリアなど、アプリ内で特定のタスクが完了したタイミングがこうした切り替わりポイントになります。ユーザーも流れの中断を想定しているため、インタースティシャル広告を表示してもユーザー エクスペリエンスを損ないません。インタースティシャル広告をアプリワークフローのどの時点で表示するか、ユーザーがどう反応する可能性があるかを考慮しましょう。
インタースティシャル広告を表示する際には必ずアクションを一時停止してください。
インタースティシャル広告には、テキスト、イメージ、動画などのさまざまな種類があります。アプリがインタースティシャル広告を表示する際に、広告がそのリソースを活用できるように、一部のリソースの使用を一時停止することも重要です。たとえばインタースティシャル広告を呼び出して表示する際は、アプリの音声出力を一時停止します。adDidDismissFullScreenContent: イベント ハンドラで音声の再生を再開できます。イベント ハンドラはユーザーが広告を操作し終わると呼び出されます。さらに、広告の表示中は、負荷の高い計算タスク(ゲームループなど)を一時的に停止することを検討してください。これにより、グラフィックが遅い、または応答しない、動画が途切れるなどの問題を回避できます。
十分な読み込み時間を確保しましょう。
インタースティシャル広告を適切なタイミングで表示することが重要であるのと同様に、読み込みの待ち時間を短くすることも重要です。表示する前に広告を読み込むようにすると、インタースティシャル広告を表示する際の準備が整います。
過度に広告を表示しないよう注意しましょう。
インタースティシャル広告の表示頻度を増やすことで収益の向上が見込める一方、ユーザー エクスペリエンスが損なわれ、クリック率の低下につながります。ユーザーがアプリを楽しめなくなるほど頻繁に広告を表示しないでください。
読み込み完了コールバックを使用してインタースティシャルを表示しないでください。
これは、ユーザー エクスペリエンスの低下につながる可能性があります。代わりに、表示が必要になる前に広告をプリロードします。次に、GAMInterstitialAdcanPresentFromRootViewController:error: メソッドを確認して、表示の準備が整っているかどうかを確認します。

GitHub の例

次のステップ