插页式广告是全屏广告,它会覆盖整个应用界面,直到用户将其关闭。这些广告通常会在应用流程的自然过渡点(例如,活动之间或游戏关卡之间的暂停时段)展示。当应用展示插页式广告时,用户可以点按广告前往其目标页面,也可以关闭广告回到应用界面。案例研究。
本指南介绍了如何将插页式广告植入到 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”消息,则表示该插页式广告仍在加载或加载失败。为避免出现此警告,请先使用 isReady
方法检查该插页式广告是否已做好展示准备,然后再调用 presentFromRootViewController:
。
使用 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