ข้อกำหนดเบื้องต้น
ตั้งค่าเหตุการณ์ที่กําหนดเองให้เสร็จสมบูรณ์
ขอโฆษณาคั่นระหว่างหน้า
เมื่อรายการโฆษณาเหตุการณ์ที่กําหนดเองไปถึงในเชนสื่อกลางแบบน้ำตก
ระบบจะเรียกใช้เมธอด 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) } }
Objective-C
#import "SampleCustomEvent.h" @implementation SampleCustomEvent SampleCustomEventInterstitial *sampleInterstitial; - (void)loadInterstitialForAdConfiguration: (GADMediationInterstitialAdConfiguration *)adConfiguration completionHandler: (GADMediationInterstitialLoadCompletionHandler) completionHandler { sampleInterstitial = [[SampleCustomEventInterstitial alloc] init]; [sampleInterstitial loadInterstitialForAdConfiguration:adConfiguration completionHandler:completionHandler]; }
SampleCustomEventInterstitial มีหน้าที่รับผิดชอบงานต่อไปนี้
โหลดโฆษณาคั่นระหว่างหน้าและเรียกใช้a
GADMediationInterstitialAdLoadCompletionHandlerเมธอดเมื่อโหลดเสร็จสมบูรณ์ใช้โปรโตคอล
GADMediationInterstitialAdรับและรายงานการเรียกกลับของเหตุการณ์โฆษณาไปยัง Google Mobile Ads SDK
ระบบจะรวมพารามิเตอร์ที่ไม่บังคับซึ่งกำหนดไว้ใน UI ไว้ในการกำหนดค่าโฆษณา
คุณเข้าถึงพารามิเตอร์ได้ผ่าน
adConfiguration.credentials.settings[@"parameter"] โดยปกติแล้วพารามิเตอร์นี้จะเป็นตัวระบุหน่วยโฆษณาที่ SDK ของเครือข่ายโฆษณาต้องใช้เมื่อสร้างอินสแตนซ์ออบเจ็กต์โฆษณา
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() } } }
Objective-C
#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 สำหรับพารามิเตอร์ข้อผิดพลาด แต่หากไม่สำเร็จ ให้ส่งผ่านข้อผิดพลาดที่พบ
โดยปกติแล้ว คุณจะใช้เมธอดเหล่านี้ภายใน Callback จาก SDK ของบุคคลที่สามที่อะแดปเตอร์ใช้ สำหรับตัวอย่างนี้ SDK ตัวอย่างมี SampleInterstitialAdDelegate พร้อม Callback ที่เกี่ยวข้อง ดังนี้
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) } }
Objective-C
- (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() } }
Objective-C
- (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 ที่ส่งกลับมาเพื่อส่งต่อเหตุการณ์การแสดงผลจาก SDK ของบุคคลที่สามไปยัง Google Mobile Ads SDK ได้ คลาส SampleCustomEventInterstitial ใช้โปรโตคอล SampleInterstitialAdDelegate เพื่อส่งต่อ Callback จากเครือข่ายโฆษณาตัวอย่างไปยัง Google Mobile Ads SDK
เหตุการณ์ที่กําหนดเองควรส่งต่อ Callback เหล่านี้ให้ได้มากที่สุด เพื่อให้แอปได้รับเหตุการณ์ที่เทียบเท่าจาก Google Mobile Ads SDK ตัวอย่างการใช้ Callback มีดังนี้
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() }
Objective-C
- (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 คุณสามารถใช้ตัวอย่างนี้กับเครือข่ายโฆษณาที่รองรับอยู่แล้วหรือแก้ไขเพื่อแสดงโฆษณาคั่นระหว่างหน้าเหตุการณ์ที่กําหนดเองก็ได้