插页式广告

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

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

前提条件

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

务必用测试广告进行测试

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

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

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

如需详细了解移动广告 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()

    do {
      interstitial = try await GAMInterstitialAd.load(
        withAdUnitID: "/6499/example/interstitial", request: GAMRequest())
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

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

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GAMInterstitialAd.load(
        withAdUnitID: "/6499/example/interstitial", request: GAMRequest())
      interstitial?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }

  /// 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

@import GoogleMobileAds;
@import UIKit;

@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 是一次性对象。也就是说,插页式广告一经展示便无法再次展示。最佳做法是在 GADFullScreenContentDelegate 上的 adDidDismissFullScreenContent: 方法中加载另一个插页式广告,以便在上一个插页式广告关闭后,立即开始加载下一个插页式广告。

展示广告

插页式广告应在应用流程的自然停顿期间进行展示,例如游戏的不同关卡之间或用户完成一项任务之后,这些都是非常不错的展示时机。以下示例展示了如何在 UIViewController 的某个操作方法中执行此操作:

Swift

@IBAction func doSomething(_ sender: Any) {
  guard let interstitial = interstitial else {
    return print("Ad wasn't ready.")
  }

  // The UIViewController parameter is an optional.
  interstitial.present(fromRootViewController: nil)
}

Objective-C

- (IBAction)doSomething:(id)sender {
  if (self.interstitial) {
    // The UIViewController parameter is nullable.
    [self.interstitial presentFromRootViewController:nil];
  } else {
    NSLog(@"Ad wasn't ready");
  }
}

如果您未获得任何返回广告,并且收到错误响应“Request Error: No ads to show”,请确保您的订单项包含定位到正确尺寸的广告素材。插页式广告尺寸分别为 320x480 和 480x320(适用于手机)和 1024x768 和 768x1024(适用于平板电脑)。如果设备不够大,无法容纳 1024x768 或 768x1024 的尺寸,就会回退到 320x480 或 480x320 的尺寸。

最佳实践

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

GitHub 上的示例

后续步骤