菜单

自定义基础适配器

本文档基于Demo中 DemoCustomBaseAdapter 示例代码,详细介绍如何创建自定义基础适配器。

1. 推荐的文件结构

复制代码
├── Base/                           # 基础适配器模块
│   ├── DemoCustomAdapterCommonHeader.h    # 用于声明一些公共的头文件
│   ├── DemoCustomBaseAdapter.h            # 基础适配器头文件
│   ├── DemoCustomBaseAdapter.m            # 基础适配器实现,用于定义指定初始发适配器的类名,和定义一些工具方法
│   ├── DemoCustomInitAdapter.h            # 初始化适配器头文件
│   └── DemoCustomInitAdapter.m            # 初始化适配器实现
  • 温馨提示:类名可以自定义,基础适配器的类名不需要添加至后台。

2. 实现步骤

2.1 实现DemoCustomInitAdapter.h

步骤:

  1. 引入头文件#import <AnyThinkSDK.h>
  2. 继承ATBaseInitAdapter
    最终效果如下:
objc 复制代码
#import <Foundation/Foundation.h>
#import "DemoCustomAdapterCommonHeader.h"
 
@interface DemoCustomInitAdapter : ATBaseInitAdapter
 
@end

2.2 实现DemoCustomInitAdapter.m

步骤:
1.实现以下方法:

objc 复制代码
/// Init Ad SDK
/// - Parameter adInitArgument: server info
- (void)initWithInitArgument:(ATAdInitArgument *)adInitArgument{
    //从adInitArgument对象中拿取后台配置的信息,例如adInitArgument.serverContentDic[@"appId"]

    //拿取到相关信息后,调用第三方SDK的初始化方法进行初始化

    //如果第三方SDK初始化有返回值,使用[self notificationNetworkInitSuccess];通知我们初始化成功;如果第三方初始化失败,使用[self notificationNetworkInitFail:error];通知我们初始化失败。

    //如果第三方SDK初始化没有成功或者失败,可直接通知我们初始化成功,使用:[self notificationNetworkInitSuccess];
}

/// 返回第三方 SDK 的版本号
- (nullable NSString *)sdkVersion {
    //例如
    return [MSAdSDK getVersionName];
}

/// 返回适配器版本号
- (nullable NSString *)adapterVersion {
    return @"2.6.4.0";
}

2.3 实现DemoCustomBaseAdapter.h

步骤:
1.引入头文件#import <AnyThinkSDK.h>
2.继承ATBaseMediationAdapter
3.如果需要支持客户端竞价(C2S),添加以下方法作为接口:

objc 复制代码
@interface DemoCustomBaseAdapter : ATBaseMediationAdapter

//C2S flow needed
+ (NSMutableDictionary *)getLossInfoResult:(ATBidWinLossResult *)winLossResult;

//C2S flow needed
+ (NSMutableDictionary *)getWinInfoResult:(ATBidWinLossResult *)winLossResult;

//C2S flow needed
+ (NSMutableDictionary *)getC2SInfo:(NSInteger)ecpm;
 
@end

2.4 实现实现DemoCustomBaseAdapter.m

步骤:
1.实现以下方法:

objc 复制代码
#pragma mark - adapter init class name define
- (Class)initializeClassName {
    //返回文档 2.1 中创建的初始化适配器类
    return [DemoCustomInitAdapter class];
}

//如果需要支持客户端竞价(C2S),添加以下方法实现:
#pragma mark - tools
+ (NSMutableDictionary *)getC2SInfo:(NSInteger)ecpm {
    //组装我们聚合SDK 需要的 C2S 竞价信息,并返回
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionary];
    
    NSString *priceStr = [NSString stringWithFormat:@"%ld",ecpm];
    NSString *logOriginalString = [NSString stringWithFormat:@"MS:C2S Original priceStr :%@",priceStr];
    [ATAdLogger logMessage:logOriginalString type:ATLogTypeExternal];
    if ([priceStr doubleValue] < 0) {
        priceStr = @"0";
    }
    
    [infoDic AT_setDictValue:priceStr key:ATAdSendC2SBidPriceKey];
    //请注意单位
    [infoDic AT_setDictValue:@(ATBiddingCurrencyTypeCNYCents) key:ATAdSendC2SCurrencyTypeKey];
    NSString *logString = [NSString stringWithFormat:@"[Network:C2S]::MS::%@",infoDic];
    [ATAdLogger logMessage:logString type:ATLogTypeInternal];

    return infoDic;
}

+ (NSMutableDictionary *)getLossInfoResult:(ATBidWinLossResult *)winLossResult {
    //组装第三方 SDK 需要的竞败信息,检查输入正确后返回,供第三方需要时使用。信息可通过winLossResult对象取出。
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionary]; 
    [infoDic AT_setDictValue:winLossResult.winPrice key:kMSAdMediaWinPrice];
    //竞败原因
    [infoDic AT_setDictValue:@"xxx" key:kMSAdMediaLossReason];
    //竞胜方渠道ID
    [infoDic AT_setDictValue:@"xxx" key:kMSAdMediaWinADN];
    return infoDic;
}

+ (NSMutableDictionary *)getWinInfoResult:(ATBidWinLossResult *)winLossResult {
    //组装第三方 SDK 需要的竞胜信息,通常是返回第二高价格。
    NSMutableDictionary *infoDic = [NSMutableDictionary dictionary];
    [infoDic AT_setDictValue:winLossResult.secondPrice key:kMSAdMediaLossPrice];
    return infoDic;
}
最近修改: 2025-09-23Powered by