插页式广告

插页式广告是全屏广告,它会覆盖整个应用界面, 被用户关闭它们通常在 例如 activity 之间或应用之间的暂停期间 游戏关卡当应用展示插页式广告时,用户可以自行选择 用户点击广告并继续访问其目标网址,或者关闭广告并返回 。 案例研究

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

前提条件

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

始终使用测试广告进行测试

在构建和测试应用时,请务必使用测试广告, 实际投放的广告。否则,可能会导致您的账号被暂停。

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

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

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

实现

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

  1. 加载广告。
  2. 注册回调。
  3. 展示广告并处理奖励事件。

加载广告

广告的加载是使用静态 loadWithAdManagerAdUnitID:request:completionHandler: 方法(针对 GAMInterstitialAd 类。加载方法需要 广告单元 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 是一次性对象。这个 是指插页式广告在展示后就不能再展示了。最佳 加载另一个插页式广告 adDidDismissFullScreenContent: 方法(在 GADFullScreenContentDelegate 上), 与上一个插页式广告相同时,下一个插页式广告 已关闭。

展示广告

插页式广告应在应用流程的自然停顿期间 (以游戏的不同关卡之间或用户完成一项任务后为例)。 以下示例展示了如何在 UIViewController:

Swift

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

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

Objective-C

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

最佳做法

考虑插页式广告是否适合您的应用。
在具有自然过渡点的应用中,插页式广告的效果最好。 应用内任务的结束,例如分享图片或完成某项任务 游戏关卡,就会产生这样一个点。因为用户希望在 用户可以轻松展示插页式广告, 体验。请务必考虑在应用工作流的哪些时间点 展示插页式广告以及用户可能会如何响应。
务必在展示插页式广告时暂停操作。
插页式广告有多种类型:文字广告、图片广告 视频等务必要确保您的应用在显示 但它也会暂停使用某些资源 请充分利用它们例如,当您调用 插页式广告时,请务必暂停应用产生的所有音频输出。 您可以在以下位置继续播放声音: adDidDismissFullScreenContent: 事件处理脚本,将在用户完成互动时调用 。此外,请考虑暂时停止所有密集计算 任务(例如游戏循环)。这样可以确保 确保用户不会遇到图像无响应、响应慢或卡顿的现象 视频。
留出充足的加载时间。
正如确保在展示位置中展示插页式广告 设置适当的时间,同样重要的是确保用户无需 等它们加载完毕在您打算展示广告之前,提前加载广告 可以确保您的应用在 是时候展示一下了。
不要向用户展示太多广告。
虽然提高插页式广告在应用中的展示频次似乎 例如增加收入的好方法,但这也会影响用户体验 和较低的点击率。确保用户不会过于频繁地 打断了他们,导致他们无法继续使用您的应用。
请勿使用加载完成回调展示插页式广告。
这可能会导致用户体验不佳。您应先预加载广告 显示所需的输出。然后检查 canPresentFromRootViewController:error: 方法 以了解是否可以在GAMInterstitialAd中使用 。

GitHub 上的示例

后续步骤