菜单

自定义插屏广告适配器

本文档基于 DemoCustomInterstitialAdapter 示例代码,详细介绍如何创建自定义插屏广告适配器,实现第三方广告平台的插屏广告接入。

1. 推荐的文件结构

复制代码
Interstitial/
├── DemoCustomInterstitialAdapter.h          # 插屏适配器头文件
├── DemoCustomInterstitialAdapter.m          # 插屏适配器实现文件
├── DemoCustomInterstitialDelegate.h         # 插屏代理头文件
└── DemoCustomInterstitialDelegate.m         # 插屏代理实现文件
  • 温馨提示:类名可以自定义,但需要与后台配置的自定义广告平台类名匹配,例如后台添加的适配器类名为:DemoCustomInterstitialAdapter,则应对应创建DemoCustomInterstitialAdapter.m文件

2. 实现步骤

2.1 实现DemoCustomInterstitialDelegate.h

步骤:

  1. 导入了头文件#import <AnyThinkSDK.h>
  2. 添加属性:@property (nonatomic, strong) ATInterstitialAdStatusBridge * adStatusBridge;
  3. 遵循您的第三方插屏广告回调协议,例如本例中是MSInterstitialDelegate

最终效果如下:

objc 复制代码
#import <Foundation/Foundation.h>
#import "DemoCustomAdapterCommonHeader.h"

@interface DemoCustomInterstitialDelegate : NSObject<MSInterstitialDelegate>

@property (nonatomic, strong) ATInterstitialAdStatusBridge * adStatusBridge;

@end

2.2 实现DemoCustomInterstitialDelegate.m

步骤:

  1. 实现您的第三方 SDK 的协议方法,例如广告加载成功,广告点击,广告关闭等。
  2. 在第一步的协议方法的广告加载成功事件中,调用通知我们广告加载成功,例如:
objc 复制代码
/**
   平台广告展示准备就绪,可以进行展示 在此回调中调用show接口展示广告
 */
- (void)msInterstitialAdReadySuccess:(MSInterstitialAd *)msInterstitialAd { //这是第三方 SDK 的协议方法,广告加载成功时调用

    NSDictionary * extraDic = [DemoCustomBaseAdapter getC2SInfo:[msInterstitialAd ecpm]];//如果需要实现客户端竞价(C2S),则您需要将第三方广告的价格回传给我们 SDK
    //通知我们广告加载成功,如果需要实现客户端竞价(C2S),将含有价格的字典传入给我们
    [self.adStatusBridge atOnInterstitialAdLoadedExtra:extraDic];
}
  1. 在第一步的协议方法的广告加载失败事件中,调用通知我们广告加载失败,例如:
objc 复制代码
/**
 *  广告预加载失败回调
 *  详解:当接收服务器返回的广告数据失败后调用该函数
 */
- (void)msInterstitialError:(MSInterstitialAd *)msInterstitialAd
                      error:(NSError *)error {//这是第三方 SDK 的协议方法,广告加载失败时调用
    //通知我们加载失败
    [self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}
  1. 参考我们SDKATInterstitialTrackProtocol协议与ATBaseTrackProtocol协议,实现其他广告事件并通知我们,例如:
objc 复制代码
/**
 *  插屏广告点击回调
 */
- (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];
}
......

2.3 实现DemoCustomInterstitialAdapter.h

步骤:

  1. 继承基础 Adapter,本例中是:DemoCustomBaseAdapter,如果不清楚如何实现它,请参考[自定义基础适配器接入文档]章节。
  2. 导入了头文件#import <AnyThinkSDK.h>
  3. 遵循ATBaseInterstitialAdapterProtocol协议

2.4 实现DemoCustomInterstitialAdapter.m

步骤:

  1. 添加以下属性:
objc 复制代码
@interface DemoCustomInterstitialAdapter()

// 本文档2.1中实现的
@property (nonatomic, strong) DemoCustomInterstitialDelegate * interstitialDelegate;

// 第三方SDK 的插屏广告对象
@property (nonatomic, strong) MSInterstitialAd *interstitialAd;

@end
  1. 初始化interstitialDelegate属性
objc 复制代码
#pragma mark - lazy
- (DemoCustomInterstitialDelegate *)interstitialDelegate{
    if (_interstitialDelegate == nil) {
        _interstitialDelegate = [[DemoCustomInterstitialDelegate alloc] init];
        _interstitialDelegate.adStatusBridge = self.adStatusBridge;
    }
    return _interstitialDelegate;
}
  1. 实现广告加载方法:
objc 复制代码
#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];
}
  1. 实现广告展示方法:
objc 复制代码
#pragma mark - Ad show
- (void)showInterstitialInViewController:(UIViewController *)viewController {
    [self.interstitialAd showAdFromRootViewController:viewController];
}
  1. 实现广告准备状态检查方法:
objc 复制代码
#pragma mark - Ad ready
- (BOOL)adReadyInterstitialWithInfo:(NSDictionary *)info {
    return self.interstitialAd.isAdValid;
}
  1. 如果需要实现客户端竞价,则添加以下方法的实现:
objc 复制代码
#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];
}

3. 广告事件回传接口列表

请前往广告事件回传接口列表

最近修改: 2025-09-23Powered by