在 iPad 上支持多个窗口

从 iOS 13 开始,应用可以支持多个 窗口 意味着用户可以同时与多个同时在 iPad 上的应用副本互动 界面。每个窗口都可以采用不同的尺寸创建,并且可以随时调整大小 而这会对广告的加载和展示方式产生影响

本指南旨在介绍呈现广告的最佳做法 在 iPad 多窗口场景中都能正常运行。

前提条件

在广告请求中设置场景

要接收适合特定窗口的广告,您需要将视图的 windowScene。Google 移动广告 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 移动广告 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.