Quảng cáo xen kẽ có tặng thưởng là một loại định dạng quảng cáo có tặng thưởng, cho phép bạn thêm phần thưởng vào những quảng cáo tự động xuất hiện tại các điểm chuyển tiếp tự nhiên của ứng dụng. Không giống như quảng cáo có tặng thưởng, người dùng không bắt buộc phải chọn xem quảng cáo xen kẽ có tặng thưởng.
Điều kiện tiên quyết
- SDK Quảng cáo của Google trên thiết bị di động phiên bản 7.60.0 trở lên.
- Xem hết Hướng dẫn bắt đầu sử dụng.
Triển khai
Sau đây là các bước chính để tích hợp quảng cáo xen kẽ có tặng thưởng:
- Tải một quảng cáo
- [Không bắt buộc] Xác thực lệnh gọi lại SSV
- Đăng ký các lệnh gọi lại
- Hiển thị quảng cáo và xử lý sự kiện phần thưởng
Tải một quảng cáo
Bạn có thể hoàn thành việc tải một quảng cáo bằng cách sử dụng
phương thức loadWithAdUnitID:request:completionHandler:
tĩnh cho
lớp GADRewardedInterstitialAd
. Phương thức tải này yêu cầu mã đơn vị quảng cáo của bạn,
một đối tượng GADRequest
và một trình xử lý hoàn thành
để gọi khi tải quảng cáo thành công hoặc không thành công. Đối tượng GADRewardedInterstitialAd
đã tải được cung cấp dưới dạng một thông số trong trình xử lý hoàn thành. Ví dụ sau cho biết cách tải một GADRewardedInterstitialAd
trong
lớp ViewController
.
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
} 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'>ca-app-pub-3940256099942544/6978759866</var>"
request:[GADRequest request]
completionHandler:^(
GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
NSError* _Nullable error) {
if (!error) {
self.rewardedInterstitialAd = rewardedInterstitialAd;
}
}
];
}
[Không bắt buộc] Xác thực lệnh gọi lại của tính năng xác minh phía máy chủ (SSV)
Những ứng dụng cần có thêm dữ liệu trong lệnh gọi lại của tính năng xác minh phía máy chủ nên sử dụng
tính năng dữ liệu tuỳ chỉnh của quảng cáo có tặng thưởng. Bất kỳ giá trị chuỗi nào được đặt cho đối tượng
quảng cáo có tặng thưởng đều sẽ được chuyển đến tham số truy vấn custom_data
cho lệnh gọi lại của SSV. Nếu bạn không đặt
giá trị dữ liệu tuỳ chỉnh, thì giá trị tham số truy vấn custom_data
sẽ không
hiển thị trong lệnh gọi lại của SSV.
Mã mẫu sau đây minh hoạ cách đặt dữ liệu tuỳ chỉnh cho đối tượng quảng cáo xen kẽ có tặng thưởng trước khi yêu cầu quảng cáo.
Swift
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
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:@"ca-app-pub-3940256099942544/6978759866"
request:GADRequest
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;
}];
Đăng ký các lệnh gọi lại
Để nhận thông báo cho các sự kiện trình bày, bạn phải triển khai
giao thức GADFullScreenContentDelegate
và chỉ định giao thức đó cho
thuộc tính fullScreenContentDelegate
của quảng cáo được trả về. Giao thức
GADFullScreenContentDelegate
xử lý các lệnh gọi lại khi quảng cáo
hiển thị thành công hoặc không thành công và khi quảng cáo bị đóng. Mã sau đây
cho biết cách triển khai giao thức và chỉ định giao thức đó cho quảng cáo:
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController, GADFullScreenContentDelegate {
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
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.")
}
}
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.");
}
Hiển thị quảng cáo và xử lý sự kiện phần thưởng
Khi hiển thị quảng cáo, bạn phải cung cấp một đối tượng GADUserDidEarnRewardHandler
để xử lý phần thưởng cho người dùng.
Bạn nên sử dụng đoạn mã sau đây để hiển thị quảng cáo xen kẽ có tặng thưởng.
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.
}
}
Objective-C
- (void)show {
// The UIViewController parameter is nullable.
[_rewardedInterstitialAd presentFromRootViewController:nil
userDidEarnRewardHandler:^{
GADAdReward *reward =
self.rewardedInterstitialAd.adReward;
// TODO: Reward the user.
}];
}
Các bước tiếp theo
Tìm hiểu thêm về quyền riêng tư của người dùng.