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
- 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ý sử dụng lệnh gọi lại
- Hiển thị quảng cáo và xử lý sự kiện tặng thưởng
Tải một quảng cáo
Bạn có thể tải một quảng cáo bằng cách sử dụng phương thức load(adUnitID:request)
trên lớp GADRewardedInterstitialAd
.
Swift
SwiftUI
import GoogleMobileAds
class RewardedInterstitialViewModel: NSObject, ObservableObject,
FullScreenContentDelegate
{
@Published var coins = 0
private var rewardedInterstitialAd: RewardedInterstitialAd?
func loadAd() async {
do {
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
with: "ca-app-pub-3940256099942544/6978759866", request: Request())
rewardedInterstitialAd?.fullScreenContentDelegate = self
} catch {
print(
"Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
}
Objective-C
Thay thế adUnitID bằng mã đơn vị quảng cáo của bạn.
[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 cơ chế 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
Objective-C
Thay thế SAMPLE_CUSTOM_DATA_STRING bằng dữ liệu tuỳ chỉnh của bạn.
Đăng ký sử dụng 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 chỉ định GADFullScreenContentDelegate
cho thuộc tính fullScreenContentDelegate
của quảng cáo được trả về:
Swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
SwiftUI
rewardedInterstitialAd?.fullScreenContentDelegate = self
Objective-C
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
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ị loại bỏ. Đoạn 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
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("\(#function) called.")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
print("\(#function) called.")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called.")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called.")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called.")
// Clear the rewarded interstitial ad.
rewardedInterstitialAd = nil
}
func ad(
_ ad: FullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("\(#function) called with error: \(error.localizedDescription).")
}
SwiftUI
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func ad(
_ ad: FullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("\(#function) called")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
// Clear the rewarded interstitial ad.
rewardedInterstitialAd = nil
}
Objective-C
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
// Clear the rewarded interstitial ad.
self.rewardedInterstitialAd = nil;
}
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}
Hiển thị quảng cáo và xử lý sự kiện tặng 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 showRewardedInterstitialAd() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
// The UIViewController parameter is an optional.
rewardedInterstitialAd.present(from: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
// TODO: Reward the user.
}
}
SwiftUI
Theo dõi các sự kiện trên giao diện người dùng trong khung hiển thị để hiển thị quảng cáo.
var rewardedInterstitialBody: some View {
// ...
}
.onChange(
of: showAd,
perform: { newValue in
if newValue {
viewModel.showAd()
}
}
)
Trình bày quảng cáo xen kẽ có tặng thưởng từ mô hình hiển thị:
func showAd() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
rewardedInterstitialAd.present(from: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward amount: \(reward.amount)")
self.addCoins(reward.amount.intValue)
}
}
Objective-C
- (void)showRewardedInterstitialAd {
[self.rewardedInterstitialAd presentFromRootViewController:self
userDidEarnRewardHandler:^{
GADAdReward *reward = self.rewardedInterstitialAd.adReward;
NSString *rewardMessage = [NSString
stringWithFormat:@"Reward received with "
@"currency %@ , amount %ld",
reward.type, [reward.amount longValue]];
NSLog(@"%@", rewardMessage);
// TODO: Reward the user.
}];
}
Ví dụ trên GitHub
Xem ví dụ đầy đủ về quảng cáo xen kẽ có tặng thưởng bằng ngôn ngữ bạn muốn:
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.