插页式广告是全屏广告,它会覆盖整个应用界面,直到用户将其关闭。这些广告通常会在应用流程的自然过渡点(例如,活动之间或游戏关卡之间的暂停时段)展示。当应用展示插页式广告时,用户可以选择点按广告,访问其目标网址,也可以将其关闭,返回应用。
本指南介绍了如何将插页式广告植入到 iOS 应用中。
前提条件
- 导入 Google 移动广告 SDK(可以只导入其自身,也可以将其作为 Firebase 的一部分加以导入)。
创建插页式广告对象
插页式广告由 GADInterstitial
对象来请求和展示。要使用此对象,第一步是对其实例化并设置其广告单元 ID。例如,以下示例演示了如何在 UIViewController
的 viewDidLoad
方法中创建 GADInterstitial
:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { var interstitial: GADInterstitial! override func viewDidLoad() { super.viewDidLoad() interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") } }
Objective-C
@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADInterstitial *interstitial; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"]; }
GADInterstitial
是一次性对象,只会加载和展示一个插页式广告。要展示多个插页式广告,则需要应用为每个广告都创建一个 GADInterstitial
。
应始终使用测试广告进行测试
在开发和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的帐号被暂停。
对于 iOS 插页式广告,最简便的测试广告加载方法是使用下面的专用测试广告单元 ID:
ca-app-pub-3940256099942544/4411468910
该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告,您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只需确保您会在发布应用前用自己的广告单元 ID 替换该测试广告单元 ID 即可。
如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试广告。
加载广告
要加载插页式广告,请在 GADRequest
对象上调用 loadRequest:
:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { var interstitial: GADInterstitial! override func viewDidLoad() { super.viewDidLoad() interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") let request = GADRequest() interstitial.load(request) } }
Objective-C
@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADInterstitial *interstitial; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"]; GADRequest *request = [GADRequest request]; [self.interstitial loadRequest:request]; }
展示广告
插页式广告应在应用流程的自然停顿期间(例如,在游戏的不同关卡之间或者在用户完成一项任务之后)展示。要展示插页式广告,请检查 GADInterstitial
上的 isReady
属性以验证加载已完成,然后再调用 presentFromRootViewController
。以下示例演示了如何在 UIViewController
中的一个操作方法中执行此操作:
Swift
@IBAction func doSomething(_ sender: AnyObject) { ... if interstitial.isReady { interstitial.present(fromRootViewController: self) } else { print("Ad wasn't ready") } }
Objective-C
- (IBAction)doSomething:(id)sender { ... if (self.interstitial.isReady) { [self.interstitial presentFromRootViewController:self]; } else { NSLog(@"Ad wasn't ready"); } }
如显示“Cannot present interstitial. It is not ready”消息,则表示该插页式广告仍在加载或加载失败。要避免出现此警告,请在调用 presentFromRootViewController:
之前使用 isReady
方法检查该插页式广告是否已做好展示准备。
使用 GADInterstitialDelegate 重新加载
GADInterstitial
是一次性对象,这意味着插页式广告展示后,hasBeenUsed
会返回 true
,就不能再用该插页式广告对象加载另一个广告了。要请求另一个插页式广告,您需要创建一个新的 GADInterstitial
对象。如果您尝试重复使用插页式广告对象,则会收到如下错误响应:“Request Error: Will not send request because interstitial object has been used”。
分配另一个插页式广告的最佳位置是在 GADInterstitialDelegate
上的 interstitialDidDismissScreen
方法中,以便在上一个插页式广告关闭后,即开始加载下一个插页式广告。您甚至可以考虑将插页式广告的初始化细分到其自身的辅助方法中:
Swift
override func viewDidLoad() { super.viewDidLoad() interstitial = createAndLoadInterstitial() } func createAndLoadInterstitial() -> GADInterstitial { var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") interstitial.delegate = self interstitial.load(GADRequest()) return interstitial } func interstitialDidDismissScreen(_ ad: GADInterstitial) { interstitial = createAndLoadInterstitial() }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; self.interstitial = [self createAndLoadInterstitial]; } - (GADInterstitial *)createAndLoadInterstitial { GADInterstitial *interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"]; interstitial.delegate = self; [interstitial loadRequest:[GADRequest request]]; return interstitial; } - (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial { self.interstitial = [self createAndLoadInterstitial]; }
在上一个插页式广告关闭之后,立即预加载另一个插页式广告,这样您的应用就能提前准备好在下一个逻辑断点再次展示插页式广告。
广告事件
通过使用 GADInterstitialDelegate
,您可以监听广告生命周期事件,例如,广告何时关闭、用户何时离开应用等。
注册插页式广告事件
要注册插页式广告事件,请将 GADInterstitial
上的 delegate
属性设置为实现 GADInterstitialDelegate
协议的对象。一般情况下,实现插页式广告的类也充当代理类,在这种情况下,可将 delegate
属性设为 self
,如下所示:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController, GADInterstitialDelegate { var interstitial: GADInterstitial! override func viewDidLoad() { super.viewDidLoad() interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910") interstitial.delegate = self } }
Objective-C
#import "GADInterstitial.h" #import "GADInterstitialDelegate.h" @interface ViewController () <GADInterstitialDelegate> @property(nonatomic, strong) GADInterstitial *interstitial; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:"ca-app-pub-3940256099942544/4411468910"]; self.interstitial.delegate = self; }
实现插页式广告事件
GADInterstitialDelegate
中的每个方法都是可选方法,因此您只需实现所需的方法即可。以下示例实现了每个方法并将消息记录到控制台:
Swift
/// Tells the delegate an ad request succeeded. func interstitialDidReceiveAd(_ ad: GADInterstitial) { print("interstitialDidReceiveAd") } /// Tells the delegate an ad request failed. func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) { print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)") } /// Tells the delegate that an interstitial will be presented. func interstitialWillPresentScreen(_ ad: GADInterstitial) { print("interstitialWillPresentScreen") } /// Tells the delegate the interstitial is to be animated off the screen. func interstitialWillDismissScreen(_ ad: GADInterstitial) { print("interstitialWillDismissScreen") } /// Tells the delegate the interstitial had been animated off the screen. func interstitialDidDismissScreen(_ ad: GADInterstitial) { print("interstitialDidDismissScreen") } /// Tells the delegate that a user click will open another app /// (such as the App Store), backgrounding the current app. func interstitialWillLeaveApplication(_ ad: GADInterstitial) { print("interstitialWillLeaveApplication") }
Objective-C
/// Tells the delegate an ad request succeeded. - (void)interstitialDidReceiveAd:(GADInterstitial *)ad { NSLog(@"interstitialDidReceiveAd"); } /// Tells the delegate an ad request failed. - (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error { NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]); } /// Tells the delegate that an interstitial will be presented. - (void)interstitialWillPresentScreen:(GADInterstitial *)ad { NSLog(@"interstitialWillPresentScreen"); } /// Tells the delegate the interstitial is to be animated off the screen. - (void)interstitialWillDismissScreen:(GADInterstitial *)ad { NSLog(@"interstitialWillDismissScreen"); } /// Tells the delegate the interstitial had been animated off the screen. - (void)interstitialDidDismissScreen:(GADInterstitial *)ad { NSLog(@"interstitialDidDismissScreen"); } /// Tells the delegate that a user click will open another app /// (such as the App Store), backgrounding the current app. - (void)interstitialWillLeaveApplication:(GADInterstitial *)ad { NSLog(@"interstitialWillLeaveApplication"); }
一些最佳做法
- 考虑插页式广告是否为适合您应用的恰当广告类型。
- 在具有自然过渡点的应用中,插页式广告的效果最好。此类过渡点通常存在于应用内的任务结束时,例如分享完图片或完成一个游戏关卡时。用户希望可以在操作过程中休息一下,因此这时展示插页式广告不会影响用户体验。请务必考虑在应用流程的哪些时间点展示插页式广告,以及用户可能会以什么方式响应。
- 务必在展示插页式广告时暂停操作。
- 插页式广告类型多样,包括文字广告、图片广告和视频广告等。确保应用在展示插页式广告时,也会暂停使用某些资源,以便供广告使用,这一点十分重要。例如,当您发出展示插页式广告的调用后,请务必暂停应用产生的所有音频输出。您可以在
interstitialDidDismissScreen
事件处理程序中恢复声音播放,该处理程序会在用户结束与广告的互动之后被调用。此外,请考虑在展示广告时暂停所有密集计算任务,例如游戏循环。这将确保用户不会遇到图像无响应、响应慢或视频卡顿的现象。 - 留出充足的加载时间。
- 确保在恰当的时间展示插页式广告十分重要,同样,确保用户无需等待广告的加载也十分重要。在您打算调用
presentFromRootViewController
前,请事先通过调用loadRequest
加载广告,这可确保应用在广告展示时间到来前完全加载插页式广告。 - 不要向用户展示太多广告。
- 虽然提高插页式广告在应用中的展示频率似乎是增加收入的绝佳方式,但这么做也会影响用户体验并降低点击率。确保用户不会频繁受到干扰,使其可以享受使用应用的过程。
- 请勿使用
interstitialDidReceiveAd
事件展示插页式广告。 - 这可能会导致用户体验不佳。不过,您可以在需要展示相应广告之前进行预加载,然后检查
GADInterstitial
上的isReady
方法,以了解广告是否已做好展示准备。
其他资源
GitHub 上的示例
- 插页式广告示例: Swift | Objective-C