必要條件
完成自訂事件設定。
請求插頁式廣告
當刊登序列中介服務鏈執行到自訂事件委刊項,系統會以您在建立自訂事件時指定的類別名稱,呼叫 loadInterstitial:adConfiguration:completionHandler:
方法。在本例中,該方法位於 SampleCustomEvent
,會接著呼叫 SampleCustomEventInterstitial
中的 loadInterstitial:adConfiguration:completionHandler:
方法。
如要請求插頁式廣告,請建立或修改實作 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
負責處理以下工作:
載入插頁式廣告,並於載入完畢時叫用
GADMediationInterstitialAdLoadCompletionHandler
方法。實作
GADMediationInterstitialAd
通訊協定。接收廣告事件回呼,並回報給 Google Mobile Ads SDK。
在 使用者介面中定義的選用參數會包含在廣告設定中。使用 adConfiguration.credentials.settings[@"parameter"]
即可存取。這項參數通常是廣告單元 ID,廣告聯播網 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 the 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
值;如果失敗,請傳遞您遇到的錯誤。
這些方法通常是在轉接程式導入的第三方 SDK 回呼中執行。以本例來說,範例 SDK 的 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) } }
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
通訊協定,將回呼從範例廣告聯播網轉送到 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() }
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 上有完整的範例可參考。範例可直接用於支援的廣告聯播網,您也可修改現有範例,顯示自訂事件插頁式廣告。