插页式广告是全屏广告,它会覆盖整个应用界面,直到用户将其关闭。这些广告通常会在应用流程的自然过渡点展示,例如,活动之间或游戏关卡之间的暂停时段。当应用展示插页式广告时,用户可以选择点按广告并继续访问其目标网址,也可以将其关闭并返回应用。 案例研究。
本指南介绍了如何将插页式广告植入到 iOS 应用中。
前提条件
- Google 移动广告 SDK 8.0.0 或更高版本。
- 通读入门指南。
务必用测试广告进行测试
在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的帐号被暂停。
对于 iOS 插页式广告,加载测试广告最简便的方法就是使用下面的专用测试广告单元 ID:
ca-app-pub-3940256099942544/4411468910
该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只是一定要在发布应用前用您自己的广告单元 ID 替换该测试广告单元 ID。
如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试广告。
实施步骤
植入插页式广告的主要步骤如下所示:
- 加载广告。
- 注册回调。
- 展示广告并处理奖励事件。
加载广告
广告的加载是通过对 GADInterstitialAd
类使用静态 loadWithAdUnitID:request:completionHandler:
方法完成的。该加载方法需要使用您的广告单元 ID、GADRequest
对象以及在广告加载成功或失败时调用的完成处理程序。已加载的 GADInterstitialAd
对象会以完成处理程序中的参数的形式提供。以下示例展示了如何在 ViewController
类中加载 GADInterstitialAd
。
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { private var interstitial: GADInterstitialAd? override func viewDidLoad() { super.viewDidLoad() let request = GADRequest() GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: request, completionHandler: { [self] ad, error in if let error = error { print("Failed to load interstitial ad with error: \(error.localizedDescription)") return } interstitial = ad } ) } }
Objective-C
@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADInterstitialAd *interstitial; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; GADRequest *request = [GADRequest request]; [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910" request:request completionHandler:^(GADInterstitialAd *ad, NSError *error) { if (error) { NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]); return; } self.interstitial = ad; }]; }
注册回调
为了接收有关展示事件的通知,您必须实现 GADFullScreenContentDelegate
协议,并将其分配给所返回广告的 fullScreenContentDelegate
属性。GADFullScreenContentDelegate
协议会在广告成功展示或展示失败,以及用户关闭广告时处理回调。以下代码展示了如何实现该协议并将其分配给广告:
Swift
class ViewController: UIViewController, GADFullScreenContentDelegate { private var interstitial: GADInterstitialAd? override func viewDidLoad() { super.viewDidLoad() let request = GADRequest() GADInterstitialAd.load(withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: request, completionHandler: { [self] ad, error in if let error = error { print("Failed to load interstitial ad with error: \(error.localizedDescription)") return } interstitial = ad interstitial?.fullScreenContentDelegate = self } ) } /// 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
@interface ViewController ()<GADFullScreenContentDelegate> @property(nonatomic, strong) GADInterstitialAd *interstitial; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; GADRequest *request = [GADRequest request]; [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910" request:request completionHandler:^(GADInterstitialAd *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."); }
GADInterstitialAd
是一次性对象。也就是说,插页式广告一经展示便无法再次展示。最佳做法是在 GADFullScreenContentDelegate
上的 adDidDismissFullScreenContent:
方法中加载另一个插页式广告,以便在上一个插页式广告关闭后,立即开始加载下一个插页式广告。
展示广告
插页式广告应在应用流程的自然停顿期间进行展示,例如游戏的不同关卡之间或用户完成一项任务之后,这些都是非常不错的展示时机。以下示例展示了如何在 UIViewController
的一个操作方法中执行此操作:
Swift
@IBAction func doSomething(_ sender: Any) { if interstitial != nil { interstitial.present(fromRootViewController: self) } else { print("Ad wasn't ready") } }
Objective-C
- (IBAction)doSomething:(id)sender { ... if (self.interstitial) { [self.interstitial presentFromRootViewController:self]; } else { NSLog(@"Ad wasn't ready"); } }
最佳实践
- 考虑插页式广告是不是适合您应用的广告类型。
- 在具有自然过渡点的应用中,插页式广告的效果最好。此类过渡点通常存在于应用内的任务结束时,例如分享完图片或完成一个游戏关卡时。用户希望可以在操作过程中休息一下,因此这时展示插页式广告不会影响用户体验。请务必考虑在应用流程的哪些时间点展示插页式广告,以及用户可能会以何种方式响应。
- 请务必在展示插页式广告时暂停操作。
- 插页式广告类型多样,包括文字广告、图片广告和视频广告等。确保应用在展示插页式广告时,也会暂停使用某些资源,以便供广告使用,这一点十分重要。例如,当您发出展示插页式广告的调用后,请务必暂停应用产生的所有音频输出。您可以在
adDidDismissFullScreenContent:
事件处理脚本中恢复声音播放,当用户结束与广告的互动时,就会调用以下处理脚本。此外,请考虑在广告展示时暂停所有密集计算任务(例如游戏循环)。这样可以确保用户不会遇到图像无响应、响应慢或视频卡顿的现象。 - 留出充足的加载时间。
- 确保在恰当的时间展示插页式广告十分重要,同样,确保用户无需等待广告加载也十分重要。在您打算展示广告之前提前加载广告,可确保应用在广告展示时间到来时,已准备好加载完毕的插页式广告。
- 不要向用户展示太多广告。
- 虽然提高插页式广告在应用中的展示频次似乎是实现增收的好方法,但这么做也会影响用户体验并降低点击率。应确保用户不会频繁受到广告打扰,使他们可以充分享受到使用应用的乐趣。
- 请勿使用加载完成回调展示插页式广告。
- 这可能会导致用户体验不佳。不过,您可以在需要展示广告之前预加载广告。然后,检查
GADInterstitialAd
中的canPresentFromRootViewController:error:
方法,确认该广告是否已做好展示准备。
其他资源
GitHub 上的示例
- 插页式广告示例: Swift | Objective-C