Prasyarat
Selesaikan penyiapan peristiwa kustom.
Meminta iklan reward
Saat item baris peristiwa kustom dicapai dalam rantai mediasi waterfall, metode loadRewarded:adConfiguration:completionHandler: dipanggil pada nama class yang Anda berikan saat membuat peristiwa kustom. Dalam hal ini,
metode tersebut berada di SampleCustomEvent
, yang kemudian memanggil
metode loadRewarded:adConfiguration:completionHandler:
di
SampleCustomEventRewarded
.
Untuk meminta iklan reward, buat atau ubah class yang mengimplementasikan
GADMediationAdapter
dan loadRewarded:adConfiguration:completionHandler:
. Jika
class yang memperluas GADMediationAdapter
sudah ada, terapkan
loadRewarded:adConfiguration:completionHandler:
di sana. Selain itu,
buat class baru untuk menerapkan GADMediationRewardedAd
.
Dalam contoh peristiwa kustom,
SampleCustomEvent
mengimplementasikan
antarmuka GADMediationAdapter
, lalu mendelegasikan ke
SampleCustomEventRewarded
.
Swift
import GoogleMobileAds class SampleCustomEvent: NSObject, GADMediationAdapter { fileprivate var rewardedAd: SampleCustomEventRewarded? ... func loadRewarded( for adConfiguration: GADMediationRewardedAdConfiguration, completionHandler: @escaping GADMediationRewardedLoadCompletionHandler ) { self.rewardedAd = SampleCustomEventRewarded() self.rewardedAd?.loadRewarded( for: adConfiguration, completionHandler: completionHandler) } }
Objective-C
#import "SampleCustomEvent.h" @implementation SampleCustomEvent ... SampleCustomEventRewarded *sampleRewarded; - (void)loadRewardedForAdConfiguration: (GADMediationRewardedAdConfiguration *)adConfiguration completionHandler: (GADMediationRewardedLoadCompletionHandler) completionHandler { sampleRewarded = [[SampleCustomEventRewarded alloc] init]; [sampleRewarded loadRewardedForAdConfiguration:adConfiguration completionHandler:completionHandler]; }
SampleCustomEventRewarded
bertanggung jawab atas tugas berikut:
Memuat iklan reward.
Mengimplementasikan protokol
GADMediationRewardedAd
.Menerima dan melaporkan callback peristiwa iklan ke Google Mobile Ads SDK.
Parameter opsional yang ditentukan di UI Ad Manager
disertakan dalam konfigurasi iklan.
Parameter ini dapat diakses melalui
adConfiguration.credentials.settings[@"parameter"]
. Parameter ini
biasanya adalah ID unit iklan yang diperlukan SDK jaringan iklan saat
membuat instance objek iklan.
Swift
class SampleCustomEventRewarded: NSObject, GADMediationRewardedAd { /// The Sample Ad Network rewarded ad. var nativeAd: SampleRewarded? /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK. var delegate: GADMediationRewardedAdEventDelegate? /// Completion handler called after ad load. var completionHandler: GADMediationRewardedLoadCompletionHandler? func loadRewarded( for adConfiguration: GADMediationRewardedAdConfiguration, completionHandler: @escaping GADMediationRewardedLoadCompletionHandler ) { rewarded = SampleRewarded.init( adUnitID: adConfiguration.credentials.settings["parameter"] as? String) rewarded?.delegate = self let adRequest = SampleAdRequest() adRequest.testMode = adConfiguration.isTestRequest self.completionHandler = completionHandler rewarded?.fetchAd(adRequest) } }
Objective-C
#import "SampleCustomEventRewarded.h" @interface SampleCustomEventRewarded () <SampleRewardedAdDelegate, GADMediationRewardedAd> { /// The sample rewarded ad. SampleRewarded *_rewardedAd; /// The completion handler to call when the ad loading succeeds or fails. GADMediationRewardedLoadCompletionHandler _loadCompletionHandler; /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK. id <GADMediationRewardedAdEventDelegate> _adEventDelegate; } @end - (void)loadRewardedAdForAdConfiguration:(GADMediationRewardedAdConfiguration *)adConfiguration completionHandler: (GADMediationRewardedLoadCompletionHandler)completionHandler { __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT; __block GADMediationRewardedLoadCompletionHandler originalCompletionHandler = [completionHandler copy]; _loadCompletionHandler = ^id<GADMediationRewardedAdEventDelegate>( _Nullable id<GADMediationRewardedAd> ad, NSError *_Nullable error) { // Only allow completion handler to be called once. if (atomic_flag_test_and_set(&completionHandlerCalled)) { return nil; } id<GADMediationRewardedAdEventDelegate> delegate = nil; if (originalCompletionHandler) { // Call original handler and hold on to its return value. delegate = originalCompletionHandler(ad, error); } // Release reference to handler. Objects retained by the handler will also be released. originalCompletionHandler = nil; return delegate; }; NSString *adUnit = adConfiguration.credentials.settings[@"parameter"]; _rewardedAd = [[SampleRewardedAd alloc] initWithAdUnitID:adUnit]; _rewardedAd.delegate = self; SampleAdRequest *adRequest = [[SampleAdRequest alloc] init]; adRequest.testMode = adConfiguration.isTestRequest; [_rewardedAd fetchAd:adRequest]; }
Baik iklan berhasil diambil atau mengalami error, Anda
akan memanggil GADMediationRewardedLoadCompletionHandler
. Jika berhasil, teruskan class yang mengimplementasikan GADMediationRewardedAd
dengan nilai nil
untuk parameter error; jika gagal, teruskan error yang Anda alami.
Biasanya, metode ini diterapkan di dalam callback dari
SDK pihak ketiga yang diterapkan adaptor Anda. Untuk contoh ini, Sample SDK
memiliki SampleRewardedAdDelegate
dengan callback yang relevan:
Swift
func rewardedDidLoad(_ interstitial: SampleRewarded) { if let handler = completionHandler { delegate = handler(self, nil) } } func rewarded( rewarded: SampleRewarded, didFailToLoadAdWith errorCode: SampleErrorCode ) { let error = SampleCustomEventUtils.SampleCustomEventErrorWithCodeAndDescription( code: SampleCustomEventErrorCode .SampleCustomEventErrorAdLoadFailureCallback, description: "Sample SDK returned an ad load failure callback with error code: \(errorCode)" ) if let handler = completionHandler { delegate = handler(nil, error) } }
Objective-C
- (void)rewardedDidLoad:(SampleRewarded *)rewarded { _adEventDelegate = _loadCompletionHandler(self, nil); } - (void)rewarded:(SampleInterstitial *)rewarded didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode { NSError *error = SampleCustomEventErrorWithCodeAndDescription( SampleCustomEventErrorAdLoadFailureCallback, [NSString stringWithFormat:@"Sample SDK returned an ad load failure " @"callback with error code: %@", errorCode]); _adEventDelegate = _loadCompletionHandler(nil, error); }
GADMediationrewardedAd
memerlukan penerapan metode present(viewController:)
untuk menampilkan iklan:
Swift
func present(from viewController: UIViewController) { if let rewarded = rewarded, rewarded.isRewardedLoaded { rewarded.show() } }
Objective-C
- (void)presentFromViewController:(UIViewController *)viewController { if ([_rewardedAd isRewardedLoaded]) { [_rewardedAd show]; } else { NSError *error = SampleCustomEventErrorWithCodeAndDescription( SampleCustomEventErrorAdNotLoaded, [NSString stringWithFormat: @"The rewarded ad failed to present because the ad was not loaded."]); [_adEventDelegate didFailToPresentWithError:error] } }
Meneruskan peristiwa mediasi ke Google Mobile Ads SDK
Setelah Anda memanggil GADMediationRewardedLoadCompletionHandler
dengan iklan
yang dimuat, objek delegasi GADMediationRewardedAdEventDelegate
yang ditampilkan kemudian
dapat digunakan oleh adaptor untuk meneruskan peristiwa presentasi dari SDK
pihak ketiga ke Google Mobile Ads SDK. Class SampleCustomEventRewarded
menerapkan protokol SampleRewardedAdDelegate
untuk meneruskan callback dari
contoh jaringan iklan ke Google Mobile Ads SDK.
Peristiwa kustom Anda harus meneruskan callback ini sebanyak mungkin, sehingga aplikasi Anda menerima peristiwa yang setara ini dari Google Mobile Ads SDK. Berikut adalah contoh penggunaan callback:
Swift
func rewardedAdDidPresent(_ rewarded: SampleRewardedAd) { delegate?.willPresentFullScreenVideo() delegate?.didStartVideo() } func rewardedAdUserDidEarnReward(_ rewarded: SampleRewardedAd) { GADAdReward aReward = GADAdReward("", rewarded) delegate.didRewardUser() }
Objective-C
- (void)rewardedAdDidPresent:(SampleRewardedAd *)rewardedAd { [_adEventDelegate willPresentFullScreenView]; [_adEventDelegate didStartVideo]; } - (void)rewardedAd:(nonnull SampleRewardedAd *)rewardedAd userDidEarnReward:(NSUInteger)reward { GADAdReward *aReward = [[GADAdReward alloc] initWithRewardType:@"" rewardAmount:[NSDecimalNumber numberWithUnsignedInt:reward]]; [_adEventDelegate didRewardUserWithReward]; }
Ini melengkapi implementasi peristiwa kustom untuk iklan reward. Contoh lengkap tersedia di GitHub. Anda dapat menggunakannya dengan jaringan iklan yang sudah didukung atau mengubahnya untuk menampilkan iklan reward peristiwa kustom.