前提条件
完成自定义事件设置。
请求横幅广告
当到达广告瀑布流中介链中的自定义事件订单项时,
loadBanner:adConfiguration:completionHandler:
方法在
您在创建自定义参数时提供的类名
事件。在此示例中
该方法位于 SampleCustomEvent
中,然后由后者调用
调用 loadBanner:adConfiguration:completionHandler:
方法,
SampleCustomEventBanner
。
要请求横幅广告,请创建或修改
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
。如果成功
传递使用 nil
值实现 GADMediationBannerAd
的类
为错误参数指定值;如果失败,请传递您
错误。
通常,这些方法是在您的适配器所实现的第三方 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 移动广告 SDK。SampleCustomEventBanner
类会实现
SampleBannerAdDelegate
协议,用于转发示例广告的回调
添加到 Google 移动广告 SDK。
您的自定义事件必须尽可能多地转发这些回调,以便您的应用从 Google 移动广告 SDK 接收这些等效事件。以下是使用回调的示例:
Swift
func bannerWillLeaveApplication(_ banner: SampleBanner) { delegate?.reportClick() }
Objective-C
- (void)bannerWillLeaveApplication:(SampleBanner *)banner { [_adEventDelegate reportClick]; }
到这里,我们已经实现针对横幅广告的自定义事件。GitHub 上提供了完整的示例。您可在已获支持的广告联盟上直接使用这些示例代码,也可在修改后用于展示自定义事件横幅广告。