插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户无需自行选择即可观看插页式激励广告。
前提条件
- Google 移动广告 SDK 7.60.0 或更高版本。
- 通读入门指南。
实施步骤
植入插页式激励广告的主要步骤如下所示:
- 加载广告
- [可选] 验证 SSV 回调
- 注册回调
- 展示广告并处理奖励事件
加载广告
广告的加载是通过对 GADRewardedInterstitialAd
类使用静态 loadWithAdUnitID:request:completionHandler:
方法完成的。该加载方法需要使用您的广告单元 ID、GADRequest
对象以及在广告加载成功或失败时调用的完成处理程序。已加载的 GADRewardedInterstitialAd
对象会以完成处理程序中的参数的形式提供。以下示例展示了如何在 ViewController
类中加载 GADRewardedInterstitialAd
。
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { private var rewardedInterstitialAd: GADRewardedInterstitialAd? override func viewDidLoad() { super.viewDidLoad() GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866", request: GADRequest()) { ad, error in if let error = error { return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } self.rewardedInterstitialAd = ad } } }
Objective-C
#import "ViewController.h" @interface ViewController () @property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [GADRewardedInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866" request:[GADRequest request] completionHandler:^( GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd, NSError* _Nullable error) { if (!error) { self.rewardedInterstitialAd = rewardedInterstitialAd; } } ]; }
[可选] 验证服务器端验证 (SSV) 回调
对于需要服务器端验证回调中额外数据的应用,应使用激励广告的自定义数据功能。在激励广告对象上设置的任何字符串值都将传递给 SSV 回调的 custom_data
查询参数。如果未设置自定义数据值,custom_data
查询参数值不会出现在 SSV 回调中。
以下代码示例演示了如何在请求广告之前对插页式激励广告对象设置自定义数据。
Swift
GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866", request: request, completionHandler: { [self] ad, error in if let error != error { rewardedInterstitialAd = ad let options = GADServerSideVerificationOptions() options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING" rewardedInterstitialAd.serverSideVerificationOptions = options }
Objective-C
GADRequest *request = [GADRequest request]; [GADRewardedInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866" request:request completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { if (error) { // Handle Error return; } self.rewardedInterstitialAd = ad; GADServerSideVerificationOptions *options = [[GADServerSideVerificationOptions alloc] init]; options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING"; ad.serverSideVerificationOptions = options; }];
注册回调
为了接收有关展示事件的通知,您必须实现 GADFullScreenContentDelegate
协议,并将其分配给所返回广告的 fullScreenContentDelegate
属性。GADFullScreenContentDelegate
协议会在广告成功展示或展示失败,以及用户关闭广告时处理回调。以下代码展示了如何实现该协议并将其分配给广告:
Swift
class ViewController: UIViewController { private var rewardedInterstitialAd: GADRewardedInterstitialAd? override func viewDidLoad() { super.viewDidLoad() GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866", request: GADRequest()) { ad, error in if let error = error { return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } self.rewardedInterstitialAd = ad self.rewardedInterstitialAd?.fullScreenContentDelegate = self } } } extension ViewController: GADFullScreenContentDelegate { /// 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) GADRewardedInterstitialAd *rewardedInterstitialAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [GADRewardedInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866" request:[GADRequest request] completionHandler:^( GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd, NSError *_Nullable error) { if (!error) { self.rewardedInterstitialAd = rewardedInterstitialAd; self.rewardedInterstitialAd.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."); }
展示广告并处理奖励事件
展示广告时,您必须提供一个 GADUserDidEarnRewardHandler
对象来处理用户奖励。
以下代码演示了展示插页式激励广告的最佳方法。
Swift
func show() { guard let rewardedInterstitialAd = rewardedInterstitialAd else { return print("Ad wasn't ready.") } rewardedInterstitialAd.present(fromRootViewController: self) { let reward = rewardedInterstitialAd.adReward // TODO: Reward the user! } }
Objective-C
- (void)show { [_rewardedInterstitialAd presentFromRootViewController:self userDidEarnRewardHandler:^{ GADAdReward *reward = self.rewardedInterstitialAd.adReward; // TODO: Reward the user! }]; }
后续步骤
详细了解用户隐私权。