本文档基于 DemoCustomInterstitialAdapter
示例代码,详细介绍如何创建自定义插屏广告适配器,实现第三方广告平台的插屏广告接入。
Interstitial/
├── DemoCustomInterstitialAdapter.h # 插屏适配器头文件
├── DemoCustomInterstitialAdapter.m # 插屏适配器实现文件
├── DemoCustomInterstitialDelegate.h # 插屏代理头文件
└── DemoCustomInterstitialDelegate.m # 插屏代理实现文件
步骤:
#import <AnyThinkSDK.h>
@property (nonatomic, strong) ATInterstitialAdStatusBridge * adStatusBridge;
MSInterstitialDelegate
最终效果如下:
#import <Foundation/Foundation.h>
#import "DemoCustomAdapterCommonHeader.h"
@interface DemoCustomInterstitialDelegate : NSObject<MSInterstitialDelegate>
@property (nonatomic, strong) ATInterstitialAdStatusBridge * adStatusBridge;
@end
步骤:
/**
平台广告展示准备就绪,可以进行展示 在此回调中调用show接口展示广告
*/
- (void)msInterstitialAdReadySuccess:(MSInterstitialAd *)msInterstitialAd { //这是第三方 SDK 的协议方法,广告加载成功时调用
NSDictionary * extraDic = [DemoCustomBaseAdapter getC2SInfo:[msInterstitialAd ecpm]];//如果需要实现客户端竞价(C2S),则您需要将第三方广告的价格回传给我们 SDK
//通知我们广告加载成功,如果需要实现客户端竞价(C2S),将含有价格的字典传入给我们
[self.adStatusBridge atOnInterstitialAdLoadedExtra:extraDic];
}
/**
* 广告预加载失败回调
* 详解:当接收服务器返回的广告数据失败后调用该函数
*/
- (void)msInterstitialError:(MSInterstitialAd *)msInterstitialAd
error:(NSError *)error {//这是第三方 SDK 的协议方法,广告加载失败时调用
//通知我们加载失败
[self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}
ATInterstitialTrackProtocol
协议与ATBaseTrackProtocol
协议,实现其他广告事件并通知我们,例如:/**
* 插屏广告点击回调
*/
- (void)msInterstitialClicked:(MSInterstitialAd *)msInterstitialAd {
[self.adStatusBridge atOnAdClick:nil];
}
/**
* 插屏广告视图展示成功回调
* 详解: 插屏广告展示成功回调该函数
*/
- (void)msInterstitialShow:(MSInterstitialAd *)msInterstitialAd {
[self.adStatusBridge atOnAdShow:nil];
}
/**
* 插屏广告展示结束回调
* 详解: 插屏广告展示结束回调该函数
*/
- (void)msInterstitialClosed:(MSInterstitialAd *)msInterstitialAd {
[self.adStatusBridge atOnAdClosed:nil];
}
/**
平台广告展示失败
详解:可能广告素材异常或三方异常导致无法广告曝光
*/
- (void)msInterstitialAdShowFail:(MSInterstitialAd *)msInterstitialAd error:(NSError *)error {
[self.adStatusBridge atOnAdShowFailed:error extra:nil];
}
/**
* 全屏广告页被关闭
*/
- (void)msInterstitialDetailClosed:(MSInterstitialAd *)msInterstitialAd {
[self.adStatusBridge atOnAdDetailClosed:nil];
}
......
步骤:
DemoCustomBaseAdapter
,如果不清楚如何实现它,请参考[自定义基础适配器接入文档]章节。#import <AnyThinkSDK.h>
ATBaseInterstitialAdapterProtocol
协议步骤:
@interface DemoCustomInterstitialAdapter()
// 本文档2.1中实现的
@property (nonatomic, strong) DemoCustomInterstitialDelegate * interstitialDelegate;
// 第三方SDK 的插屏广告对象
@property (nonatomic, strong) MSInterstitialAd *interstitialAd;
@end
interstitialDelegate
属性#pragma mark - lazy
- (DemoCustomInterstitialDelegate *)interstitialDelegate{
if (_interstitialDelegate == nil) {
_interstitialDelegate = [[DemoCustomInterstitialDelegate alloc] init];
_interstitialDelegate.adStatusBridge = self.adStatusBridge;
}
return _interstitialDelegate;
}
#pragma mark - Ad load
- (void)loadADWithArgument:(ATAdMediationArgument *)argument {
//通过argument对象获取必要的加载信息,创建好必要的参数,准备传入给第三方的插屏加载方法,开始加载广告
self.interstitialAd = [[MSInterstitialAd alloc] init];
//注意设置代理给DemoCustomInterstitialDelegate
self.interstitialAd.delegate = self.interstitialDelegate;
MSInterstitialAdConfigParams *adParam = [[MSInterstitialAdConfigParams alloc]init];
adParam.videoMuted = [argument.localInfoDic[@"video_muted"] intValue] == 0 ? NO : YES;
adParam.isNeedCloseAdAfterClick = [argument.localInfoDic[@"click_close"] intValue] == 0 ? NO : YES;
[self.interstitialAd loadAdWithPid:argument.serverContentDic[@"slot_id"] adConfigParams:adParam];
}
#pragma mark - Ad show
- (void)showInterstitialInViewController:(UIViewController *)viewController {
[self.interstitialAd showAdFromRootViewController:viewController];
}
#pragma mark - Ad ready
- (BOOL)adReadyInterstitialWithInfo:(NSDictionary *)info {
return self.interstitialAd.isAdValid;
}
#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:@"DemoCustomInterstitialAdapter sendWin"] type:ATLogTypeExternal];
NSMutableDictionary *infoDic = [DemoCustomBaseAdapter getWinInfoResult:result];
[self.interstitialAd sendWinNotificationWithInfo:infoDic];
}
- (void)sendLoss:(ATBidWinLossResult *)result { //告诉第三方 SDK 竞价失败
[ATAdLogger logMessage:[NSString stringWithFormat:@"DemoCustomInterstitialAdapter sendLoss"] type:ATLogTypeExternal];
NSString *priceStr = [self.interstitialAd mediaExt][@"ecpm"];
NSMutableDictionary *infoDict = [DemoCustomBaseAdapter getLossInfoResult:result];
[infoDict AT_setDictValue:priceStr key:kMSAdMediaWinPrice];
[self.interstitialAd sendLossNotificationWithInfo:infoDict];
}
请前往广告事件回传接口列表。