iPad での複数のウィンドウのサポート

iOS 13 以降のアプリは、 Windows できます。つまり、ユーザーはアプリの複数のコピーを同時に操作して、 UI です。各ウィンドウはさまざまなサイズで作成でき、いつでもサイズ変更できます。 これは広告の読み込み方法と表示方法に影響します

このガイドでは、広告を表示するためのおすすめの方法を紹介します。 マルチウィンドウのシナリオで正しく機能します

前提条件

広告リクエストのシーンを設定する

特定のウィンドウに収まる広告を受け取るには、ビューの 広告リクエストに windowScene。Google Mobile Ads SDK は、 サイズを指定します。

Swift

func loadInterstitial() {
  let request = GADRequest()
  request.scene = view.window?.windowScene

  GADInterstitialAd.load(withAdUnitID: "[AD_UNIT_ID]",
      request: request) { ad, error in }
}

Objective-C

- (void)loadInterstitial {
  GADRequest *request = [GADRequest request];
  request.scene = self.view.window.windowScene;

  [GADInterstitialAd loadWithAdUnitID:@"[AD_UNIT_ID]"
      request:request
      completionHandler:^(GADInterstitialAd *ad, NSError *error) {}];
}

テストモードでは、マルチシーン アプリがシーンを渡さずに広告をリクエストすると、広告リクエストは次のエラーで失敗します。

<Google> Invalid Request. The GADRequest scene property should be set for
applications that support multi-scene. Treating the unset property as an error
while in test mode.

本番環境モードでは、広告リクエストは充足されますが、 広告が全画面表示されないウィンドウに表示される。この場合のエラー メッセージは次のようになります。

<Google> Ad cannot be presented. The full screen ad content size exceeds the current window size.

viewDidAppear で広告リクエストを作成します。

マルチウィンドウの場合、ウィンドウ シーンを複数のウィンドウに 広告リクエストを送信できますビューはまだウィンドウに追加されていないため、 viewDidLoad: の場合は、代わりに viewDidAppear: で広告リクエストを作成してください その時点までにウィンドウ シーンが設定されます。

なお、viewDidAppear: は、アプリの呼び出し中に複数回呼び出されることがあります。 説明します。広告リクエストの初期化コードは、 すでに完了しているかどうかを示します。

Swift

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  if !requestInitialized {
    loadInterstitial()
    requestInitialized = true
  }
}

Objective-C

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  if (!_requestInitialized) {
    [self loadInterstitial];
    _requestInitialized = YES;
  }
}

サイズ変更を処理する

ユーザーはいつでもシーンをドラッグして、広告の表示後にウィンドウ サイズを変更できます 行われます。サイズ変更の際に、新しい広告をリクエストするかどうかは任意です。 以下のサンプルコードでは、 viewWillTransitionToSize:withTransitionCoordinator: ルートビュー コントローラのウィンドウが回転またはサイズ変更されたときに通知を受け取れるようにしますが、 また、 windowScene:didUpdateCoordinateSpace:interfaceOrientation:traitCollection: ウィンドウシーン固有の変化を確認します

インタースティシャル広告とリワード広告

Google Mobile Ads SDK には canPresentFromViewController:error:: インタースティシャルと 有効かどうかを判定し、フルスクリーン モードがあるかどうかを ウィンドウ サイズが変わるたびに広告を更新する必要がある。

Swift

override func viewWillTransition(to size: CGSize,
    with coordinator: UIViewControllerTransitionCoordinator) {
  super.viewWillTransition(to: size, with: coordinator)

  coordinator.animate(alongsideTransition: nil) { [self] context in
    do {
      try interstitial?.canPresent(fromRootViewController: self)
    } catch {
      loadInterstitial()
    }
  }
}

Objective-C

- (void)viewWillTransitionToSize:(CGSize)size
    withTransitionCoordinator:(id)coordinator {
  [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

  [coordinator animateAlongsideTransition:nil
      completion:^(id _Nonnull context) {
    if (![self.interstitial canPresentFromRootViewController:self error:nil]) {
      [self loadInterstitial];
    }
  }];
}

ウィンドウのサイズ変更は、ウィンドウの回転と同じ方法で処理できます。お客様の バナー広告が新しいウィンドウ サイズに収まるようにする必要があります。

以下の例では、新しいウィンドウの幅を持つ新しいアダプティブ バナーを作成しています。

Swift

override func viewWillTransition(to size: CGSize,
    with coordinator: UIViewControllerTransitionCoordinator) {
  super.viewWillTransition(to: size, with: coordinator)

  coordinator.animate(alongsideTransition: nil) { [self] context in
    loadBanner()
  }
}

func loadBanner() {
  let bannerWidth = view.frame.size.width

  bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(bannerWidth)

  let request = GADRequest()
  request.scene = view.window?.windowScene
  bannerView.load(request)
}

Objective-C

- (void)viewWillTransitionToSize:(CGSize)size
    withTransitionCoordinator:(id)coordinator {
  [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

  [coordinator animateAlongsideTransition:nil
      completion:^(id _Nonnull context) {
    [self loadBannerAd];
  }];
}

- (void)loadBannerAd {
  CGFloat bannerWidth = self.view.frame.size.width;

  self.bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(bannerWidth);

  GADRequest *request = [GADRequest request];
  request.scene = self.view.window.windowScene;
  [self.bannerView loadRequest:request];
}

ネイティブ広告

ネイティブ広告のレンダリングは広告主様が管理でき、 ネイティブ広告は、アプリの他の部分と同様に、サイズを変更したビュー内にレンダリングされる 説明します。

既知の問題

現在、マルチウィンドウ広告と分割画面広告は、縦表示でのみサポートされています。 横表示の広告をリクエストすると、次のログ メッセージが表示されます。

<Google> Ad cannot be presented. The full screen ad content size exceeds the
current window size.