插页式广告

插页式广告是全屏广告,它会覆盖整个应用界面,直到用户将其关闭。这些广告通常会在应用流程的自然过渡点(例如,活动之间或游戏关卡之间的暂停时段)展示。当应用展示插页式广告时,用户可以选择点按广告前往其目标页面,也可以关闭广告回到应用界面。 案例研究

本指南介绍了如何将插页式广告植入到 iOS 应用中。

前提条件

  • Google 移动广告 SDK 8.0.0 或更高版本。
  • 完成入门指南

务必用测试广告进行测试

在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的账号被中止。

对于 iOS 插页式广告,加载测试广告最简便的方法就是使用下面的测试专用广告单元 ID:
/21775744923/example/interstitial

该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只需确保您会在发布应用前用自己的广告单元 ID 替换该测试广告单元 ID 即可。

如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试广告

实现

植入插页式广告的主要步骤如下所示:

  1. 加载广告。
  2. 注册回调。
  3. 展示广告。

加载广告

广告的加载是通过对 GAMInterstitialAd 类使用 load(adUnitID:request) 方法完成的。

Swift

fileprivate func loadInterstitial() async {
  do {
    interstitial = try await AdManagerInterstitialAd.load(
      with: "/21775744923/example/interstitial", request: AdManagerRequest())
    interstitial?.fullScreenContentDelegate = self
  } catch {
    print("Failed to load interstitial ad with error: \(error.localizedDescription)")
  }
}

SwiftUI

import GoogleMobileAds

class InterstitialViewModel: NSObject, FullScreenContentDelegate {
  private var interstitialAd: InterstitialAd?

  func loadAd() async {
    do {
      interstitialAd = try await InterstitialAd.load(
        with: "ca-app-pub-3940256099942544/4411468910", request: Request())
      interstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

[GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial"
                                     request:[GAMRequest 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;
                           }];

注册回调

若要接收有关展示事件的通知,您必须为返回的广告分配 GADFullScreenContentDelegate to thefullScreenContentDelegate` 属性:

Swift

interstitial?.fullScreenContentDelegate = self

SwiftUI

interstitialAd?.fullScreenContentDelegate = self

Objective-C

self.interstitial.fullScreenContentDelegate = self;

GADFullScreenContentDelegate 协议会在广告成功展示或展示失败,以及用户关闭广告时处理回调。以下代码展示了如何实现此协议:

Swift

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
  print("\(#function) called with error: \(error.localizedDescription)")
  // Clear the interstitial ad.
  interstitial = nil
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the interstitial ad.
  interstitial = nil
}

SwiftUI

func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the interstitial ad.
  interstitialAd = nil
}

Objective-C

- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)ad:(id<GADFullScreenPresentingAd>)ad
    didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
  // Clear the interstitial ad.
  self.interstitial = nil;
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the interstitial ad.
  self.interstitial = nil;
}

GAMInterstitialAd 是一次性对象。这意味着,插页式广告一经展示便不能再次展示。最佳做法是在 GADFullScreenContentDelegate 上的 adDidDismissFullScreenContent: 方法中加载另一个插页式广告,以便在上一个插页式广告关闭后,立即开始加载下一个插页式广告。

展示广告

插页式广告应在应用流程的自然停顿期间进行展示,例如游戏的不同关卡之间或用户完成一项任务之后,都是非常不错的展示时机。

Swift

ad.present(from: self!)

SwiftUI

监听视图中的界面事件,以确定何时展示广告。

var body: some View {
  // ...
  }
  .onChange(of: countdownTimer.isComplete) { newValue in
    showGameOverAlert = newValue
  }
  .alert(isPresented: $showGameOverAlert) {
    Alert(
      title: Text("Game Over"),
      message: Text("You lasted \(countdownTimer.countdownTime) seconds"),
      dismissButton: .cancel(
        Text("OK"),
        action: {
          viewModel.showAd()
        }))

从 ViewModel 中展示插页式广告:

func showAd() {
  guard let interstitialAd = interstitialAd else {
    return print("Ad wasn't ready.")
  }

  interstitialAd.present(from: nil)
}

Objective-C

[self.interstitial presentFromRootViewController:self];

最佳做法

考虑插页式广告这种广告类型是否适合您的应用。
在具有自然过渡点的应用中,插页式广告的效果最好。此类过渡点通常存在于应用内的任务结束时,例如分享完图片或完成一个游戏关卡时。用户希望可以在操作过程中休息一下,因此这时展示插页式广告不会影响用户体验。请务必考虑在应用流程的哪些时间点展示插页式广告,以及用户可能会以什么方式响应。
务必在展示插页式广告时暂停操作。
插页式广告类型多样,包括文字广告、图片广告和视频广告等。确保应用在展示插页式广告时,也会暂停使用某些资源,以便供广告使用,这一点十分重要。例如,当您请求展示插页式广告时,请务必暂停应用产生的所有音频输出。您可以在 adDidDismissFullScreenContent: 事件处理脚本中恢复声音播放,当用户结束与广告的互动时,就会调用以下处理脚本。此外,还应考虑在广告展示时暂停所有会占用大量资源的计算任务,例如游戏循环。这样可以确保用户不会遇到图像无响应、响应慢或视频卡顿的现象。
留出充足的加载时间。
确保在恰当的时间展示插页式广告十分重要,同样,确保用户无需等待广告加载也十分重要。在您打算展示广告前,不妨事先对其进行加载,这样可确保应用在广告展示时间到来时,已准备好加载完毕的插页式广告。
不要向用户展示太多广告。
虽然提高插页式广告在应用中的展示频次似乎是实现增收的好方法,但这么做也会影响用户体验并降低点击率。应确保用户不会频繁受到广告打扰,使他们可以充分享受到使用应用的乐趣。
不要使用加载完成回调展示插页式广告。
这可能会导致用户体验不佳。应预先加载广告,以备需要展示时使用。接下来,检查 GAMInterstitialAd 中的 canPresentFromRootViewController:error: 方法,看看该广告是否已做好展示准备。

GitHub 上的示例

查看以您的首选语言显示的完整插页式广告示例:

后续步骤