Interstisial reward adalah jenis format iklan berinsentif yang memungkinkan Anda menawarkan reward untuk iklan yang muncul secara otomatis selama transisi yang biasa terjadi di aplikasi. Tidak seperti iklan reward, pengguna tidak perlu memilih untuk 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 loadWithAdUnitID:request:completionHandler:
statis di class GADRewardedInterstitialAd
. Metode pemuatan memerlukan ID unit iklan, objek GADRequest
, dan pengendali penyelesaian yang akan dipanggil saat pemuatan iklan berhasil atau gagal. Objek
GADRewardedInterstitialAd
yang dimuat diberikan sebagai parameter di pengendali
penyelesaian. Contoh di bawah ini menunjukkan cara memuat GADRewardedInterstitialAd
di
class ViewController
Anda.
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; } } ]; }
[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 akan diteruskan ke parameter kueri custom_data
dari callback SSV. Jika tidak ada nilai data kustom yang ditetapkan, parameter value kueri custom_data
tidak akan ada di callback SSV.
Contoh kode berikut menunjukkan cara menetapkan data kustom pada objek iklan interstisial reward sebelum meminta iklan.
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; }];
Mendaftar untuk callback
Agar dapat menerima notifikasi untuk peristiwa presentasi, Anda harus menerapkan protokol GADFullScreenContentDelegate
dan menetapkannya ke properti fullScreenContentDelegate
dari iklan yang ditampilkan. Protokol GADFullScreenContentDelegate
menangani callback saat iklan berhasil ditampilkan atau tidak berhasil, dan saat iklan ditutup. Kode berikut
menunjukkan cara menerapkan protokol dan menetapkannya ke iklan:
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."); }
Menampilkan iklan dan menangani peristiwa reward
Saat menampilkan iklan, Anda harus menyediakan objek GADUserDidEarnRewardHandler
untuk menangani reward bagi pengguna.
Kode berikut menunjukkan metode terbaik untuk menampilkan iklan interstisial reward.
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! }]; }
Langkah berikutnya
Pelajari privasi pengguna lebih lanjut.