پیشنیازها
راهاندازی رویدادهای سفارشی را تکمیل کنید.
درخواست آگهی بینابینی
وقتی در زنجیره میانجیگری آبشاری به مورد خطی رویداد سفارشی میرسید،
روش loadInterstitial:adConfiguration:completionHandler: در
نام کلاسی که هنگام ایجاد رویداد سفارشی ارائه کردهاید فراخوانی میشود. در این مورد،
آن روش در SampleCustomEvent است، که سپس
روش loadInterstitial:adConfiguration:completionHandler: را در
SampleCustomEventInterstitial فراخوانی میکند.
برای درخواست آگهی بینابینی، کلاسی را ایجاد یا اصلاح کنید که
GADMediationAdapter و loadInterstitial:adConfiguration:completionHandler: را پیادهسازی میکند.
اگر کلاسی که GADMediationAdapter را گسترش میدهد ازقبل وجود دارد،
loadInterstitial:adConfiguration:completionHandler: را در آنجا پیادهسازی کنید. علاوهبراین،
کلاس جدیدی برای پیادهسازی GADMediationInterstitialAd ایجاد کنید.
در مثال رویداد سفارشی ما،
SampleCustomEvent
میانای GADMediationAdapter را پیادهسازی میکند و سپس به
SampleCustomEventInterstitial واگذار میکند.
Swift
import GoogleMobileAds class SampleCustomEvent: NSObject, MediationAdapter { fileprivate var interstitialAd: SampleCustomEventInterstitial? ... func loadInterstitial( for adConfiguration: MediationInterstitialAdConfiguration, completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler ) { self.interstitialAd = SampleCustomEventInterstitial() self.interstitialAd?.loadInterstitial( for: adConfiguration, completionHandler: completionHandler) } }
آبجکتیو-سی
#import "SampleCustomEvent.h" @implementation SampleCustomEvent SampleCustomEventInterstitial *sampleInterstitial; - (void)loadInterstitialForAdConfiguration: (GADMediationInterstitialAdConfiguration *)adConfiguration completionHandler: (GADMediationInterstitialLoadCompletionHandler) completionHandler { sampleInterstitial = [[SampleCustomEventInterstitial alloc] init]; [sampleInterstitial loadInterstitialForAdConfiguration:adConfiguration completionHandler:completionHandler]; }
SampleCustomEventInterstitial مسئول تکالیف زیر است:
بار کردن آگهی بینابینی و فراخوانی روش
GADMediationInterstitialAdLoadCompletionHandlerپساز تکمیل بار کردن.درحال اجرای پروتکل
GADMediationInterstitialAd.دریافت و گزارش کردن فراخوانیهای رویداد آگهی به Google Mobile Ads SDK.
پارامتر اختیاری تعریفشده در رابط کاربری در پیکربندی آگهی گنجانده شده است.
ازطریق
adConfiguration.credentials.settings[@"parameter"] میتوانید به این پارامتر دسترسی پیدا کنید. این پارامتر معمولاً شناسه واحد آگهی است که کیت توسعه نرمافزار شبکه آگهی هنگام نمونهسازی شیء آگهی به آن نیاز دارد.
Swift
import GoogleMobileAds class SampleCustomEventInterstitial: NSObject, MediationInterstitialAd { /// The Sample Ad Network interstitial ad. var interstitial: SampleInterstitial? /// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK. var delegate: MediationInterstitialAdEventDelegate? var completionHandler: GADMediationInterstitialLoadCompletionHandler? func loadInterstitial( for adConfiguration: MediationInterstitialAdConfiguration, completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler ) { interstitial = SampleInterstitial.init( adUnitID: adConfiguration.credentials.settings["parameter"] as? String) interstitial?.delegate = self let adRequest = SampleAdRequest() adRequest.testMode = adConfiguration.isTestRequest self.completionHandler = completionHandler interstitial?.fetchAd(adRequest) } func present(from viewController: UIViewController) { if let interstitial = interstitial, interstitial.isInterstitialLoaded { interstitial.show() } } }
آبجکتیو-سی
#import "SampleCustomEventInterstitial.h" @interface SampleCustomEventInterstitial () <SampleInterstitialAdDelegate, GADMediationInterstitialAd> { /// The sample interstitial ad. SampleInterstitial *_interstitialAd; /// The completion handler to call when the ad loading succeeds or fails. GADMediationInterstitialLoadCompletionHandler _loadCompletionHandler; /// The ad event delegate to forward ad rendering events to the Google Mobile /// Ads SDK. id <GADMediationInterstitialAdEventDelegate> _adEventDelegate; } @end - (void)loadInterstitialForAdConfiguration: (GADMediationInterstitialAdConfiguration *)adConfiguration completionHandler: (GADMediationInterstitialLoadCompletionHandler) completionHandler { __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT; __block GADMediationInterstitialLoadCompletionHandler originalCompletionHandler = [completionHandler copy]; _loadCompletionHandler = ^id<GADMediationInterstitialAdEventDelegate>( _Nullable id<GADMediationInterstitialAd> ad, NSError *_Nullable error) { // Only allow completion handler to be called once. if (atomic_flag_test_and_set(&completionHandlerCalled)) { return nil; } id<GADMediationInterstitialAdEventDelegate> 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"]; _interstitialAd = [[SampleInterstitial alloc] initWithAdUnitID:adUnit]; _interstitialAd.delegate = self; SampleAdRequest *adRequest = [[SampleAdRequest alloc] init]; adRequest.testMode = adConfiguration.isTestRequest; [_interstitialAd fetchAd:adRequest]; }
چه آگهی باموفقیت واکشی شود یا با خطا مواجه شود،
GADMediationInterstitialLoadCompletionHandler را فراخوانی میکنید. درصورت موفقیت، از کلاسی که GADMediationInterstitialAd را با مقدار nil برای پارامتر خطا پیادهسازی میکند عبور کنید؛ درصورت ناموفق بودن، از خطایی که با آن مواجه شدید عبور کنید.
معمولاً این روشها در داخل فراخوانیهای برگشتی از کیت توسعه نرمافزار طرف سوم که آداپتور شما پیادهسازی میکند، پیادهسازی میشوند. برای این مثال، «کیت توسعه نرمافزار نمونه»
دارای SampleInterstitialAdDelegate با فراخوانیهای برگشتی مربوط است:
Swift
func interstitialDidLoad(_ interstitial: SampleInterstitial) { if let handler = completionHandler { delegate = handler(self, nil) } } func interstitial( _ interstitial: SampleInterstitial, didFailToLoadAdWith errorCode: SampleErrorCode ) { let error = SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription( code: SampleCustomEventErrorCodeSwift .SampleCustomEventErrorAdLoadFailureCallback, description: "Sample SDK returned an ad load failure callback with error code: \(errorCode)" ) if let handler = completionHandler { delegate = handler(nil, error) } }
آبجکتیو-سی
- (void)interstitialDidLoad:(SampleInterstitial *)interstitial { _adEventDelegate = _loadCompletionHandler(self, nil); } - (void)interstitial:(SampleInterstitial *)interstitial 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); }
GADMediationInterstitialAd برای نمایش آگهی به پیادهسازی روش present نیاز دارد:
Swift
func present(from viewController: UIViewController) { if let interstitial = interstitial, interstitial.isInterstitialLoaded { interstitial.show() } }
آبجکتیو-سی
- (void)presentFromViewController:(UIViewController *)viewController { if ([_interstitialAd isInterstitialLoaded]) { [_interstitialAd show]; } else { NSError *error = SampleCustomEventErrorWithCodeAndDescription( SampleCustomEventErrorAdNotLoaded, [NSString stringWithFormat:@"The interstitial ad failed to present " @"because the ad was not loaded."]); [_adEventDelegate didFailToPresentWithError:error] } }
بازارسال رویدادهای میانجیگری به Google Mobile Ads SDK
وقتی GADMediationInterstitialLoadCompletionHandler را با آگهی بارشده فراخوانی کردید، شیء نماینده GADMediationInterstitialAdEventDelegate برگرداندهشده میتواند توسط آداپتور برای بازارسال رویدادهای ارائه از کیت توسعه نرمافزار شخص ثالث به Google Mobile Ads SDK استفاده شود. کلاس SampleCustomEventInterstitial
پروتکل SampleInterstitialAdDelegate را برای بازارسال تماسهای برگشتی از
شبکه آگهی نمونه به Google Mobile Ads SDK پیادهسازی میکند.
مهم است که رویداد سفارشی شما تا حد امکان تعداد بیشتری از این فراخوانیهای برگشتی را بازارسال کند تا برنامهتان این رویدادهای معادل را از Google Mobile Ads SDK دریافت کند. در اینجا نمونهای از استفاده از فراخوانیهای برگشتی آورده شده است:
Swift
func interstitialWillPresentScreen(_ interstitial: SampleInterstitial) { delegate?.willPresentFullScreenView() delegate?.reportImpression() } func interstitialWillDismissScreen(_ interstitial: SampleInterstitial) { delegate?.willDismissFullScreenView() } func interstitialDidDismissScreen(_ interstitial: SampleInterstitial) { delegate?.didDismissFullScreenView() } func interstitialWillLeaveApplication(_ interstitial: SampleInterstitial) { delegate?.reportClick() }
آبجکتیو-سی
- (void)interstitialWillPresentScreen:(SampleInterstitial *)interstitial { [_adEventDelegate willPresentFullScreenView]; [_adEventDelegate reportImpression]; } - (void)interstitialWillDismissScreen:(SampleInterstitial *)interstitial { [_adEventDelegate willDismissFullScreenView]; } - (void)interstitialDidDismissScreen:(SampleInterstitial *)interstitial { [_adEventDelegate didDismissFullScreenView]; } - (void)interstitialWillLeaveApplication:(SampleInterstitial *)interstitial { [_adEventDelegate reportClick]; }
با این کار، پیادهسازی رویدادهای سفارشی برای آگهیهای بینابینی تکمیل میشود. نمونه کامل در GitHub دردسترس است. میتوانید از آن با شبکه آگهیای که ازقبل پشتیبانی میشود استفاده کنید یا آن را برای نمایش آگهیهای بینابینی رویداد سفارشی اصلاح کنید.