前提条件
カスタム イベントのセットアップを完了しておきます。
バナー広告をリクエストする
ウォーターフォール メディエーション チェーンでカスタム イベントの広告申込情報に回ってくると、
loadBanner:adConfiguration:completionHandler: メソッドがカスタム イベントの作成時に指定した
クラス名で呼び出されます。この場合、
そのメソッドはSampleCustomEventにあり、そこから
SampleCustomEventBannerのloadBanner:adConfiguration:completionHandler:メソッドが呼び出されます。
バナー広告をリクエストするには、GADMediationAdapter と loadBanner:adConfiguration:completionHandler:
を実装するクラスを作成または変更します。GADMediationAdapter
を継承するクラスがすでに存在する場合は、そこにloadBanner:adConfiguration:completionHandler:
を実装します。さらに、
新しいクラスを作成してGADMediationBannerAdを実装します。
カスタム イベントの例では、
SampleCustomEvent が
GADMediationAdapter インターフェースを実装し、その後
SampleCustomEventBanner にデリゲートします。
Swift
import GoogleMobileAds class SampleCustomEvent: NSObject, MediationAdapter { fileprivate var bannerAd: SampleCustomEventBanner? ... func loadBanner( for adConfiguration: MediationBannerAdConfiguration, completionHandler: @escaping GADMediationBannerLoadCompletionHandler ) { self.bannerAd = SampleCustomEventBanner() self.bannerAd?.loadBanner( for: adConfiguration, completionHandler: completionHandler) } }
Objective-C
#import "SampleCustomEvent.h" @implementation SampleCustomEvent ... SampleCustomEventBanner *sampleBanner; - (void)loadBannerForAdConfiguration: (GADMediationBannerAdConfiguration *)adConfiguration completionHandler:(GADMediationBannerLoadCompletionHandler) completionHandler { sampleBanner = [[SampleCustomEventBanner alloc] init]; [sampleBanner loadBannerForAdConfiguration:adConfiguration completionHandler:completionHandler]; }
SampleCustomEventBanner の役割は次のとおりです。
バナー広告を読み込み、読み込みが完了したら
GADMediationBannerLoadCompletionHandlerメソッドを呼び出す。GADMediationBannerAdプロトコルを実装する。広告イベント コールバックを受信して Google Mobile Ads SDK に報告する。
AdMob の管理画面で定義された省略可能なパラメータは、広告構成に含まれます。
このパラメータには
adConfiguration.credentials.settings[@"parameter"]を介してアクセスできます。このパラメータは通常、広告オブジェクトをインスタンス化する際に広告ネットワーク SDK が要求する広告ユニット ID
です。
Swift
class SampleCustomEventBanner: NSObject, MediationBannerAd { /// The Sample Ad Network banner ad. var bannerAd: SampleBanner? /// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK. var delegate: MediationBannerAdEventDelegate? /// Completion handler called after ad load var completionHandler: GADMediationBannerLoadCompletionHandler? func loadBanner( for adConfiguration: MediationBannerAdConfiguration, completionHandler: @escaping GADMediationBannerLoadCompletionHandler ) { // Create the bannerView with the appropriate size. let adSize = adConfiguration.adSize bannerAd = SampleBanner( frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height)) bannerAd?.delegate = self bannerAd?.adUnit = adConfiguration.credentials.settings["parameter"] as? String let adRequest = SampleAdRequest() adRequest.testMode = adConfiguration.isTestRequest self.completionHandler = completionHandler bannerAd?.fetchAd(adRequest) } }
Objective-C
#import "SampleCustomEventBanner.h" @interface SampleCustomEventBanner () <SampleBannerAdDelegate, GADMediationBannerAd> { /// The sample banner ad. SampleBanner *_bannerAd; /// The completion handler to call when the ad loading succeeds or fails. GADMediationBannerLoadCompletionHandler _loadCompletionHandler; /// The ad event delegate to forward ad rendering events to the Google Mobile /// Ads SDK. id <GADMediationBannerAdEventDelegate> _adEventDelegate; } @end @implementation SampleCustomEventBanner - (void)loadBannerForAdConfiguration: (GADMediationBannerAdConfiguration *)adConfiguration completionHandler:(GADMediationBannerLoadCompletionHandler) completionHandler { __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT; __block GADMediationBannerLoadCompletionHandler originalCompletionHandler = [completionHandler copy]; _loadCompletionHandler = ^id<GADMediationBannerAdEventDelegate>( _Nullable id<GADMediationBannerAd> ad, NSError *_Nullable error) { // Only allow completion handler to be called once. if (atomic_flag_test_and_set(&completionHandlerCalled)) { return nil; } id<GADMediationBannerAdEventDelegate> 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"]; _bannerAd = [[SampleBanner alloc] initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width, adConfiguration.adSize.size.height)]; _bannerAd.adUnit = adUnit; _bannerAd.delegate = self; SampleAdRequest *adRequest = [[SampleAdRequest alloc] init]; adRequest.testMode = adConfiguration.isTestRequest; [_bannerAd fetchAd:adRequest]; }
広告の取得が成功した場合もエラーが発生した場合も、GADMediationBannerLoadCompletionHandler
を呼び出すことになります。成功した場合は、GADMediationBannerAd を実装するクラスを、エラー パラメータに nil
値を指定して渡します。失敗した場合は、発生したエラーを渡します。
通常、これらのメソッドは、アダプタが実装するサードパーティ SDK のコールバック内に実装されます。この例では、関連するコールバックを含む
SampleBannerAdDelegate が「Sample SDK」に含まれています。
Swift
func bannerDidLoad(_ banner: SampleBanner) { if let handler = completionHandler { delegate = handler(self, nil) } } func banner( _ banner: SampleBanner, 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)bannerDidLoad:(SampleBanner *)banner { _adEventDelegate = _loadCompletionHandler(self, nil); } - (void)banner:(SampleBanner *)banner 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); }
GADMediationBannerAd には、UIView プロパティの実装が必要です。
Swift
var view: UIView { return bannerAd ?? UIView() }
Objective-C
- (nonnull UIView *)view { return _bannerAd; }
メディエーション イベントを Google Mobile Ads SDK に転送する
読み込まれた広告で GADMediationBannerLoadCompletionHandler を呼び出した後、返された GADMediationBannerAdEventDelegate デリゲート オブジェクトをアダプタが使用することにより、プレゼンテーション イベントをサードパーティ SDK から Google Mobile Ads SDK に転送できます。SampleCustomEventBanner クラスは
SampleBannerAdDelegate プロトコルを実装して、サンプル広告
ネットワークからのコールバックを Google Mobile Ads SDK に転送します。
カスタム イベントには、こうしたコールバックを可能な限り漏れなく転送させましょう。これにより、アプリは同等イベントを Google Mobile Ads SDK から受け取ることができます。コールバックの使用例を以下に示します。
Swift
func bannerWillLeaveApplication(_ banner: SampleBanner) { delegate?.reportClick() }
Objective-C
- (void)bannerWillLeaveApplication:(SampleBanner *)banner { [_adEventDelegate reportClick]; }
これで、バナー広告のカスタム イベントの実装が完了しました。サンプル全体は で入手できます GitHub。 このサンプルは、すでにサポートされている広告ネットワークの実装例として利用できるほか、必要に応じてコードをカスタマイズし、独自のバナー広告用カスタム イベントを表示するために利用することも可能です。