插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户 用户才能选择观看插页式激励广告。
前提条件
- Google 移动广告 SDK 7.60.0 或更高版本。
- 完成入门指南。
实现
植入插页式激励广告的主要步骤如下所示:
- 加载广告
- [可选] 验证 SSV 回调
- 注册回调
- 展示广告并处理奖励事件
加载广告
广告的加载是使用 load(adUnitID:request)
完成的。
(针对 GADRewardedInterstitialAd
类)。
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
Task {
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "/21775744923/example/rewarded-interstitial", request: GAMRequest())
} catch {
print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
}
}
}
SwiftUI
import GoogleMobileAds
class RewardedInterstitialViewModel: NSObject, ObservableObject,
GADFullScreenContentDelegate
{
@Published var coins = 0
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
func loadAd() async {
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
rewardedInterstitialAd?.fullScreenContentDelegate = self
} catch {
print(
"Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
}
Objective-C
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[GADRewardedInterstitialAd
loadWithAdUnitID:@"<var label='the ad unit ID'>/21775744923/example/rewarded-interstitial</var>"
request:[GAMRequest request]
completionHandler:^(
GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
NSError* _Nullable error) {
if (!error) {
self.rewardedInterstitialAd = rewardedInterstitialAd;
}
}
];
}
[可选] 验证服务器端验证 (SSV) 回调
需要在服务器端获取额外数据的应用
验证回调应使用
自定义数据功能。在激励广告对象上设置的任何字符串值都将传递给 SSV 回调的 custom_data
查询参数。如果拒绝
自定义数据值后,custom_data
查询参数值不会
。
以下代码示例演示了如何为激励广告设置自定义数据 插页式广告对象。
Swift
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "/21775744923/example/rewarded-interstitial", request: GAMRequest())
let options = GADServerSideVerificationOptions()
options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
rewardedInterstitialAd.serverSideVerificationOptions = options
} catch {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}
Objective-C
[GADRewardedInterstitialAd
loadWithAdUnitID:@"/21775744923/example/rewarded-interstitial"
request:GAMRequest
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
import GoogleMobileAds
import UIKit
class ViewController: UIViewController, GADFullScreenContentDelegate {
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
Task {
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "/21775744923/example/rewarded-interstitial", request: GAMRequest())
self.rewardedInterstitialAd?.fullScreenContentDelegate = self
} catch {
print("Failed to load rewarded 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.")
}
}
SwiftUI
将 fullScreenContentDelegate
属性分配给返回的广告:
rewardedInterstitialAd?.fullScreenContentDelegate = self
实现该协议:
func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) {
print("\(#function) called")
}
func adDidRecordClick(_ ad: GADFullScreenPresentingAd) {
print("\(#function) called")
}
func ad(
_ ad: GADFullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("\(#function) called")
}
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("\(#function) called")
}
func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("\(#function) called")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("\(#function) called")
// Clear the rewarded interstitial ad.
rewardedInterstitialAd = nil
}
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:@"/21775744923/example/rewarded-interstitial"
request:[GAMRequest 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.")
}
// The UIViewController parameter is an optional.
rewardedInterstitialAd.present(fromRootViewController: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
// TODO: Reward the user.
}
}
SwiftUI
监听视图中的界面事件以展示广告。
var rewardedInterstitialBody: some View {
// ...
}
.onChange(
of: showAd,
perform: { newValue in
if newValue {
viewModel.showAd()
}
}
)
从视图模型中展示插页式激励广告:
func showAd() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
rewardedInterstitialAd.present(fromRootViewController: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward amount: \(reward.amount)")
self.addCoins(reward.amount.intValue)
}
}
Objective-C
- (void)show {
// The UIViewController parameter is nullable.
[_rewardedInterstitialAd presentFromRootViewController:nil
userDidEarnRewardHandler:^{
GADAdReward *reward =
self.rewardedInterstitialAd.adReward;
// TODO: Reward the user.
}];
}
GitHub 上的示例
以您的首选语言查看完整的插页式激励广告示例:
后续步骤
不妨详细了解用户隐私权。