本文档基于 DemoCustomBannerAdapter
示例代码,详细介绍如何创建自定义横幅广告适配器,实现第三方广告平台的横幅广告接入。
Banner/
├── DemoCustomBannerAdapter.h # 横幅适配器头文件
├── DemoCustomBannerAdapter.m # 横幅适配器实现文件
├── DemoCustomBannerDelegate.h # 横幅代理头文件
└── DemoCustomBannerDelegate.m # 横幅代理实现文件
DemoCustomBannerAdapter
,则应对应创建DemoCustomBannerAdapter.m
文件步骤:
#import <AnyThinkSDK.h>
@property (nonatomic, strong) ATBannerAdStatusBridge * adStatusBridge;
MSBannerViewDelegate
最终效果如下:
#import <Foundation/Foundation.h>
#import "DemoCustomAdapterCommonHeader.h"
@interface DemoCustomBannerDelegate : NSObject<MSBannerViewDelegate>
@property (nonatomic, strong) ATBannerAdStatusBridge * adStatusBridge;
@end
步骤:
/**
平台广告准备就绪,可以进行展示
*/
- (void)msBannerAdReadySuccess:(MSBannerView *)msBannerAd { //这是第三方 SDK 的协议方法,广告加载成功时调用
NSDictionary * extraDic = [DemoCustomBaseAdapter getC2SInfo:[msBannerAd ecpm]];//如果需要实现客户端竞价(C2S),则您需要将第三方广告的价格回传给我们 SDK
//通知我们广告加载成功,如果需要实现客户端竞价(C2S),将含有价格的字典传入给我们
[self.adStatusBridge atOnBannerAdLoadedWithView:msBannerAd adExtra:extraDic];
}
/**
* 请求广告失败后调用
*/
- (void)msBannerError:(MSBannerView *)msBannerAd error:(NSError *)error {//这是第三方 SDK 的协议方法,广告加载失败时调用
//通知我们加载失败
[self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}
4.参考我们SDKATBannerTrackProtocol
协议与ATBaseTrackProtocol
协议,实现其他广告事件并通知我们,例如:
/**
* banner条点击回调
*/
- (void)msBannerClicked:(MSBannerView *)msBannerAd {
[self.adStatusBridge atOnAdClick:nil];
}
步骤:
DemoCustomBaseAdapter
,如果不清楚如何实现它,请参考[自定义基础适配器接入文档]章节。#import <AnyThinkSDK.h>
ATBaseBannerAdapterProtocol
协议步骤:
@interface DemoCustomBannerAdapter()
// 本文档2.1中实现的
@property (nonatomic, strong) DemoCustomBannerDelegate * bannerDelegate;
// 第三方SDK 的banner广告对象
@property (nonatomic, strong) MSBannerView *bannerView;
@end
bannerDelegate
属性#pragma mark - lazy
- (DemoCustomBannerDelegate *)bannerDelegate{
if (_bannerDelegate == nil) {
_bannerDelegate = [[DemoCustomBannerDelegate alloc] init];
_bannerDelegate.adStatusBridge = self.adStatusBridge;
}
return _bannerDelegate;
}
#pragma mark - Ad load
- (void)loadADWithArgument:(ATAdMediationArgument *)argument {
dispatch_async(dispatch_get_main_queue(), ^{ //通常需要在主线程执行
//通过argument对象获取必要的加载信息,如尺寸等,创建好必要的参数,准备传入给第三方的横幅加载方法,开始加载广告
CGSize bannerSize = CGSizeMake(320, 50);
if (!CGSizeEqualToSize(argument.bannerSize, CGSizeZero)) {
bannerSize = argument.bannerSize;
}
self.bannerView = [[MSBannerView alloc] initWithFrame:CGRectMake(0, 0, bannerSize.width, bannerSize.height)];
//注意设置代理给DemoCustomBannerDelegate
self.bannerView.delegate = self.bannerDelegate;
MSBannerAdConfigParams *adParam = [[MSBannerAdConfigParams alloc]init];
adParam.showCloseBtn = NO;
adParam.interval = 0;
[self.bannerView loadAdAndShowWithPid:argument.serverContentDic[@"slot_id"] presentVC:argument.viewController adParams:adParam];
});
}
#pragma mark - C2S Win Loss
- (void)didReceiveBidResult:(ATBidWinLossResult *)result { //接收我们 SDK 给的客户端竞价结果
if (result.bidResultType == ATBidWinLossResultTypeWin) {
[self sendWin:result];
return;
}
[self sendLoss:result];
}
- (void)sendWin:(ATBidWinLossResult *)result { //告诉第三方 SDK 竞价成功
[ATAdLogger logMessage:[NSString stringWithFormat:@"DemoCustomBannerAdapter sendWin"] type:ATLogTypeExternal];
NSMutableDictionary *infoDic = [DemoCustomBaseAdapter getWinInfoResult:result];
[self.bannerView sendWinNotificationWithInfo:infoDic];
}
- (void)sendLoss:(ATBidWinLossResult *)result { //告诉第三方 SDK 竞价失败
[ATAdLogger logMessage:[NSString stringWithFormat:@"DemoCustomBannerAdapter sendLoss"] type:ATLogTypeExternal];
NSString *priceStr = [self.bannerView mediaExt][@"ecpm"];
NSMutableDictionary *infoDict = [DemoCustomBaseAdapter getLossInfoResult:result];
[infoDict AT_setDictValue:priceStr key:kMSAdMediaWinPrice];
[self.bannerView sendLossNotificationWithInfo:infoDict];
}
请前往广告事件回传接口列表。