Interstisial reward adalah jenis format iklan berinsentif yang memungkinkan Anda menawarkan reward untuk iklan yang muncul secara otomatis selama transisi aplikasi secara alami. Berbeda dengan iklan reward, pengguna tidak diwajibkan untuk memilih melihat interstisial reward.
Prasyarat
- Google Mobile Ads SDK 7.60.0 atau yang lebih tinggi.
- Selesaikan Panduan memulai.
Penerapan
Langkah utama untuk mengintegrasikan iklan interstisial reward adalah sebagai berikut:
- Memuat iklan
- [Opsional] Memvalidasi callback SSV
- Mendaftar untuk callback
- Menampilkan iklan dan menangani peristiwa reward
Memuat iklan
Pemuatan iklan dilakukan menggunakan metode load(adUnitID:request)
di class 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: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
} 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'>ca-app-pub-3940256099942544/6978759866</var>"
request:[GADRequest request]
completionHandler:^(
GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
NSError* _Nullable error) {
if (!error) {
self.rewardedInterstitialAd = rewardedInterstitialAd;
}
}
];
}
[Opsional] Memvalidasi callback verifikasi sisi server (SSV)
Aplikasi yang memerlukan data tambahan dalam callback verifikasi
sisi server harus menggunakan
fitur data kustom iklan reward. Setiap nilai string yang ditetapkan pada objek iklan reward
diteruskan ke parameter kueri custom_data
dari callback SSV. Jika tidak ada nilai data kustom yang ditetapkan, nilai parameter kueri custom_data
tidak akan ada dalam callback SSV.
Contoh kode berikut menunjukkan cara menetapkan data kustom pada objek iklan interstisial reward sebelum meminta iklan.
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;
}];
Mendaftar untuk callback
Agar dapat menerima notifikasi untuk peristiwa presentasi, Anda harus menerapkan protokol GADFullScreenContentDelegate
dan menetapkannya ke properti fullScreenContentDelegate
iklan yang ditampilkan. Protokol
GADFullScreenContentDelegate
menangani callback saat iklan
berhasil atau tidak berhasil ditayangkan, dan saat iklan ditutup. Kode berikut
menunjukkan cara menerapkan protokol dan menetapkannya ke iklan:
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: "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.")
}
}
SwiftUI
Tetapkan properti fullScreenContentDelegate
ke iklan yang ditampilkan:
rewardedInterstitialAd?.fullScreenContentDelegate = self
Terapkan protokol:
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:@"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.");
}
Menampilkan iklan dan menangani peristiwa reward
Saat menampilkan iklan, Anda harus memberikan objek GADUserDidEarnRewardHandler
untuk menangani reward bagi pengguna.
Kode berikut menyajikan metode terbaik untuk menampilkan iklan interstisial reward.
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
Proses peristiwa UI di tampilan untuk menampilkan iklan.
var rewardedInterstitialBody: some View {
// ...
}
.onChange(
of: showAd,
perform: { newValue in
if newValue {
viewModel.showAd()
}
}
)
Sajikan iklan interstisial reward dari model tampilan:
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.
}];
}
Contoh di GitHub
Lihat contoh lengkap iklan interstisial reward dalam bahasa yang Anda inginkan:
Langkah berikutnya
Pelajari lebih lanjut privasi pengguna.