アプリ起動時広告

このガイドは、アプリ起動時広告を統合しているパブリッシャー様を対象としています。

アプリ起動時広告は、アプリの読み込み画面を収益化したいパブリッシャー向けの特別な広告フォーマットです。アプリ起動時広告は、ユーザーがいつでも閉じることができます。 アプリ起動時広告は、ユーザーがアプリをフォアグラウンド表示したときに表示されます。

アプリ起動時広告では、小さなブランド領域が自動的に表示されるため、ユーザーはアプリを操作していることが分かります。以下にアプリ起動時広告の例を示します。

アプリ起動時広告を実装するために必要な手順は次のとおりです。

  1. 広告を表示する前に読み込むマネージャー クラスを作成します。
  2. アプリのフォアグラウンド イベント中に広告を表示します。
  3. プレゼンテーションのコールバックを処理する。

前提条件

必ずテスト広告でテストする

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

以下のアプリ起動時広告専用のテスト広告ユニット ID を使用すると、テスト広告を簡単に読み込むことができます。

/21775744923/example/app-open

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

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

マネージャー クラスを実装する

広告はすぐに表示される必要があるため、表示する前に広告を読み込むことをおすすめします。そうすることで、ユーザーがアプリにアクセスしたときに、すぐに広告の準備が整います。また、マネージャー クラスを実装して、広告を表示する必要があるタイミングに先立って広告リクエストを行います。

AppOpenAdManager という新しいシングルトン クラスを作成し、次のように入力します。

Swift

class AppOpenAdManager: NSObject {
  var appOpenAd: GADAppOpenAd?
  var isLoadingAd = false.
  var isShowingAd = false

  static let shared = AppOpenAdManager()

  private func loadAd() async {
    // TODO: Implement loading an ad.
  }

  func showAdIfAvailable() {
    // TODO: Implement showing an ad.
  }

  private func isAdAvailable() -> Bool {
    // Check if ad exists and can be shown.
    return appOpenAd != nil
  }
}

Objective-C

@interface AppOpenAdManager ()
@property(nonatomic, strong) GADAppOpenAd *appOpenAd;
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;

@end

@implementation AppOpenAdManager

+ (nonnull AppOpenAdManager *)sharedInstance {
  static AppOpenAdManager *instance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    instance = [[AppOpenAdManager alloc] init];
  });
  return instance;
}

- (void)loadAd {
  // TODO: Implement loading an ad.
}

// Add this method to the .h file.
- (void)showAdIfAvailable {
  // TODO: Implement showing an ad.
}

- (BOOL)isAdAvailable {
  // Check if ad exists and can be shown.
  return self.appOpenAd != nil;
}

@end

広告を読み込む

次のステップは、loadAd() メソッドの入力です。

Swift

private func loadAd() async {
  // Do not load ad if there is an unused ad or one is already loading.
  if isLoadingAd || isAdAvailable() {
    return
  }
  isLoadingAd = true

  do {
    appOpenAd = try await GADAppOpenAd.load(
      withAdUnitID: "/21775744923/example/app-open", request: GAMRequest())
  } catch {
    print("App open ad failed to load with error: \(error.localizedDescription)")
  }
  isLoadingAd = false
}

Objective-C

- (void)loadAd {
  // Do not load ad if there is an unused ad or one is already loading.
  if (self.isLoadingAd || [self isAdAvailable]) {
    return;
  }
  self.isLoadingAd = YES;

  [GADAppOpenAd loadWithAdUnitID:@"/21775744923/example/app-open"
                       request:[GAMRequest request]
             completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
               self.isLoadingAd = NO;
               if (error) {
                 NSLog(@"Failed to load app open ad: %@", error);
                 return;
               }
               self.appOpenAd = appOpenAd;
             }];
}

広告を表示する

次のステップは、showAdIfAvailable() メソッドの入力です。使用できる広告がない場合、このメソッドは広告の読み込みを試みます。

Swift

func showAdIfAvailable() {
  // If the app open ad is already showing, do not show the ad again.
  guard !isShowingAd else { return }

  // If the app open ad is not available yet but is supposed to show, load
  // a new ad.
  if !isAdAvailable() {
    Task {
      await loadAd()
    }
    return
  }

  if let ad = appOpenAd {
    isShowingAd = true
    ad.present(fromRootViewController: nil)
  }
}

Objective-C

- (void)showAdIfAvailable {
  // If the app open ad is already showing, do not show the ad again.
  if (self.isShowingAd) {
    return;
  }

  // If the app open ad is not available yet but is supposed to show, load a
  // new ad.
  if (![self isAdAvailable]) {
    [self loadAd];
    return;
  }

  self.isShowingAd = YES;
  [self.appOpenAd presentFromRootViewController:nil];
}

アプリのフォアグラウンド イベント中に広告を表示する

アプリがアクティブになったら、showAdIfAvailable() を呼び出して広告を表示する(利用可能な場合)か、新しい広告を読み込みます。

Swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  // ...

  func applicationDidBecomeActive(_ application: UIApplication) {
    // Show the app open ad when the app is foregrounded.
    AppOpenAdManager.shared.showAdIfAvailable()
  }
}

Objective-C

@implementation AppDelegate
// ...

- (void) applicationDidBecomeActive:(UIApplication *)application {
  // Show the app open ad when the app is foregrounded.
  [AppOpenAdManager.sharedInstance showAdIfAvailable];
}

@end

プレゼンテーション コールバックを処理する

アプリでアプリ起動時広告を表示する場合は、GADFullScreenContentDelegate を使って特定のプレゼンテーション イベントを処理する必要があります。特に、最初のアプリ起動時広告が終了したら、次のアプリ起動時広告をリクエストすることをおすすめします。

AppOpenAdManager クラスに以下を追加します。

Swift

class AppOpenAdManager: NSObject, GADFullScreenContentDelegate {
  // ...

  private func loadAd() async {
    // Do not load ad if there is an unused ad or one is already loading.
    if isLoadingAd || isAdAvailable() {
      return
    }
    isLoadingAd = true

    do {
      appOpenAd = try await GADAppOpenAd.load(
        withAdUnitID: "/21775744923/example/app-open", request: GAMRequest())
      appOpenAd?.fullScreenContentDelegate = self
    } catch {
      print("App open ad failed to load with error: \(error.localizedDescription)")
    }
    isLoadingAd = false
  }

  // ...

  // MARK: - GADFullScreenContentDelegate methods

  func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("App open ad will be presented.")
  }

  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    appOpenAd = nil
    isShowingAd = false
    // Reload an ad.
    Task {
      await loadAd()
    }
  }

  func ad(
    _ ad: GADFullScreenPresentingAd,
    didFailToPresentFullScreenContentWithError error: Error
  ) {
    appOpenAd = nil
    isShowingAd = false
    // Reload an ad.
    Task {
      await loadAd()
    }
  }
}

Objective-C

@interface AppOpenAdManager () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADAppOpenAd *appOpenAd
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;

@end

@implementation AppOpenAdManager

// ...

- (void)loadAd {
  // Do not load ad if there is an unused ad or one is already loading.
  if (self.isLoadingAd || [self isAdAvailable]) {
    return;
  }
  self.isLoadingAd = YES;

  [GADAppOpenAd loadWithAdUnitID:@"/21775744923/example/app-open"
                       request:[GAMRequest request]
             completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
              self.isLoadingAd = NO;
               if (error) {
                 NSLog(@"Failed to load app open ad: %@", error);
                 return;
               }
               self.appOpenAd = appOpenAd;
               self.appOpenAd.fullScreenContentDelegate = self;
             }];
}

- (BOOL)isAdAvailable {
  // Check if ad exists and can be shown.
  return self.appOpenAd != nil;
}

// ...

#pragma mark - GADFullScreenContentDelegate methods

- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  NSLog(@"App open ad is will be presented.");
}

- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  self.appOpenAd = nil;
  self.isShowingAd = NO;
  // Reload an ad.
  [self loadAd];
}

- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
    didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
  self.appOpenAd = nil;
  self.isShowingAd = NO;
  // Reload an ad.
  [self loadAd];
}

@end

広告の有効期限を考慮する

期限切れの広告を表示しないようにするには、広告参照が読み込まれてからの経過時間を確認するメソッドをアプリのデリゲートに追加します。

AppOpenAdManagerloadTime という Date プロパティを追加し、広告が読み込まれるときにプロパティを設定します。次に、広告の読み込みから特定の時間未満の場合に true を返すメソッドを追加します。広告を表示する前に、広告参照の有効性を必ず確認してください。

Swift

class AppOpenAdManager: NSObject, GADFullScreenContentDelegate {
  var appOpenAd: GADAppOpenAd?
  var isLoadingAd = false.
  var isShowingAd = false
  var loadTime: Date?
  let fourHoursInSeconds = TimeInterval(3600 * 4)

  // ...

  private func loadAd() async {
    // Do not load ad if there is an unused ad or one is already loading.
    if isLoadingAd || isAdAvailable() {
      return
    }
    isLoadingAd = true

    do {
      appOpenAd = try await GADAppOpenAd.load(
        withAdUnitID: "/21775744923/example/app-open", request: GAMRequest())
      appOpenAd?.fullScreenContentDelegate = self
      loadTime = Date()
    } catch {
      print("App open ad failed to load with error: \(error.localizedDescription)")
    }
    isLoadingAd = false
  }

  private func wasLoadTimeLessThanFourHoursAgo() -> Bool {
    guard let loadTime = loadTime else { return false }
    // Check if ad was loaded more than four hours ago.
    return Date().timeIntervalSince(loadTime) < fourHoursInSeconds
  }

  private func isAdAvailable() -> Bool {
    // Check if ad exists and can be shown.
    return appOpenAd != nil && wasLoadTimeLessThanFourHoursAgo()
  }
}

Objective-C

static NSTimeInterval const fourHoursInSeconds = 3600 * 4;

@interface AppOpenAdManager () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADAppOpenAd *appOpenAd
@property(nonatomic, assign) BOOL isLoadingAd;
@property(nonatomic, assign) BOOL isShowingAd;
@property(weak, nonatomic) NSDate *loadTime;

@end

@implementation AppOpenAdManager

// ...

- (void)loadAd {
  // Do not load ad if there is an unused ad or one is already loading.
  if (self.isLoadingAd || [self isAdAvailable]) {
    return;
  }
  self.isLoadingAd = YES;

  [GADAppOpenAd loadWithAdUnitID:@"/21775744923/example/app-open"
                       request:[GAMRequest request]
             completionHandler:^(GADAppOpenAd *_Nullable appOpenAd, NSError *_Nullable error) {
              self.isLoadingAd = NO;
               if (error) {
                 NSLog(@"Failed to load app open ad: %@", error);
                 return;
               }
               self.appOpenAd = appOpenAd;
               self.appOpenAd.fullScreenContentDelegate = self;
               self.loadTime = [NSDate date];
             }];
}

- (BOOL)wasLoadTimeLessThanFourHoursAgo {
  // Check if ad was loaded more than four hours ago.
  return [[NSDate Date] timeIntervalSinceDate:self.loadTime] < fourHoursInSeconds;
}

- (BOOL)isAdAvailable {
  // Check if ad exists and can be shown.
  return self.appOpenAd != nil && [self wasLoadTimeLessThanFourHoursAgo];
}

@end

コールド スタートと読み込み画面

このドキュメントでは、メモリ内で停止されているアプリがフォアグラウンドで動作しているときにのみ、アプリ起動時広告を表示することを前提としています。「コールド スタート」は、アプリが起動されたが、それまでメモリ内で一時停止されていないときに発生します。

コールド スタートの例として、ユーザーが初めてアプリを開いた場合が挙げられます。 コールド スタートでは、すぐに表示可能な、以前に読み込まれたアプリ起動時広告がありません。広告をリクエストしてから広告が返されるまでに時間がかかると、ユーザーがアプリを少しの間だけ使用できる状態で、コンテキスト外の広告が表示されてしまうことがあります。ユーザー エクスペリエンスが低下するため、この方法は避けてください。

コールド スタートでアプリ起動時広告を使用する場合は、読み込み画面を使用してゲームアセットやアプリアセットを読み込み、読み込み画面からのみ広告を表示することをおすすめします。アプリの読み込みが完了し、アプリのメイン コンテンツにユーザーを誘導した場合は、広告を表示しない。

ベスト プラクティス

アプリ起動時広告は、アプリの読み込み画面を収益化できるように作られていますが、ユーザーがアプリを楽しく利用できるように、以下の点に留意することが重要です。

  • 最初のアプリ起動時広告は、ユーザーがアプリを数回使用してから表示する。
  • 本来ならユーザーがアプリの読み込みを待っているはずの時間帯に、アプリ起動時広告を表示します。
  • アプリ起動時広告の下に読み込み画面があり、広告が閉じられる前に読み込み画面の読み込みが完了した場合は、adDidDismissFullScreenContent メソッドで読み込み画面を閉じることをおすすめします。

GitHub の完全なサンプル

Swift Objective-C

次のステップ

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