前提条件
完成自定义事件设置。
请求横幅广告
在达到广告瀑布流中介链内的自定义事件订单项时,系统会对您在创建自定义事件时提供的类名称调用 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, GADMediationAdapter { fileprivate var bannerAd: SampleCustomEventBanner? ... func loadBanner( for adConfiguration: GADMediationBannerAdConfiguration, 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 移动广告 SDK
AdMob 界面中定义的可选参数包含在广告配置中。可以通过
adConfiguration.credentials.settings[@"parameter"]
。通常,此参数是实例化广告对象时广告联盟 SDK 所需的广告单元标识符。
Swift
class SampleCustomEventBanner: NSObject, GADMediationBannerAd { /// The Sample Ad Network banner ad. var bannerAd: SampleBanner? /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK. var delegate: GADMediationBannerAdEventDelegate? /// Completion handler called after ad load var completionHandler: GADMediationBannerLoadCompletionHandler? func loadBanner( for adConfiguration: GADMediationBannerAdConfiguration, 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 的回调中实现的。在本示例中,示例 SDK
具有带有相关回调的 SampleBannerAdDelegate
:
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 移动广告 SDK
使用加载的广告调用 GADMediationBannerLoadCompletionHandler
后,
然后,便可以将返回的 GADMediationBannerAdEventDelegate
委托对象
用于将展示事件从第三方 SDK 转发至
Google 移动广告 SDKSampleCustomEventBanner
类实现 SampleBannerAdDelegate
协议,可将来自示例广告联盟的回调转发到 Google 移动广告 SDK。
请务必确保您的自定义事件转发的回调数量与 这样您的应用才能从 Google 移动广告 SDK。下面是一个使用回调的示例:
Swift
func bannerWillLeaveApplication(_ banner: SampleBanner) { delegate?.reportClick() }
Objective-C
- (void)bannerWillLeaveApplication:(SampleBanner *)banner { [_adEventDelegate reportClick]; }
到这里,我们已经实现针对横幅广告的自定义事件。完整示例 可通过以下平台观看: GitHub。 您可以将其用于已获支持的广告网络,也可以将其修改为 展示自定义事件横幅广告。