通过自定义事件,您可以为不属于某个广告联盟的广告联盟添加广告瀑布流中介 支持的广告联盟。为此,您可以为要集成的广告联盟实现自定义事件适配器。
您可以在我们的 GitHub 中找到完整的示例自定义事件项目 代码库。
前提条件
您必须先将以下广告格式之一集成到您的应用中,然后才能创建自定义事件:
在界面中创建自定义事件
必须先在 AdMob 中创建自定义事件 界面。请参阅添加自定义事件中的说明。
您需要提供以下信息:
- 类名称
实现自定义事件的类的完全限定名称 适配器,例如
SampleCustomEvent
;或者,如果您的类是在 Swift 中实现的,MediationExample.SampleCustomEventSwift
。如果项目中包含多个目标,或者项目名称与目标名称不一致,则需要包含目标名称。有了目标名称后 将如下所示:
appName_targetName.className
。此外,请务必将短划线等任何非字母数字字符替换为下划线。示例。- 标签
定义广告来源的唯一名称。
- 参数
传递到自定义事件适配器的可选字符串参数。
实现 GADMediationAdapter
创建自定义事件的第一步是实现
GADMediationAdapter
协议,如 SampleCustomEvent
类所示
(如我们的示例所示)。
该类负责接收来自 AdMob 中,并将创建 正确的广告格式。
初始化适配器
在 Google 移动广告 SDK 初始化时
setUpWithConfiguration:completionHandler:
在所有受支持的第三方适配器以及配置的自定义事件上均调用
。您可以使用此方法对自定义事件所需的第三方 SDK 执行任何必要的设置或初始化操作。
Swift
import GoogleMobileAds
class SampleCustomEvent: NSObject, GADMediationAdapter {
static func setUpWith(
_ configuration: GADMediationServerConfiguration,
completionHandler: @escaping GADMediationAdapterSetUpCompletionBlock
) {
// This is where you will initialize the SDK that this custom event is built
// for. Upon finishing the SDK initialization, call the completion handler
// with success.
completionHandler(nil)
}
}
Objective-C
#import "SampleCustomEvent.h"
@implementation SampleCustomEvent
+ (void)setUpWithConfiguration:(nonnull GADMediationServerConfiguration *)configuration
completionHandler:(nonnull GADMediationAdapterSetUpCompletionBlock)completionHandler {
// This is where you initialize the SDK that this custom event is built
// for. Upon finishing the SDK initialization, call the completion handler
// with success.
completionHandler(nil);
}
报告版本号
所有自定义事件都必须向 Google 移动广告 SDK 报告自定义事件适配器本身的版本,以及与该自定义事件对接的第三方 SDK 的版本。报告版本时
GADVersionNumber
对象:
Swift
static func adSDKVersion() -> GADVersionNumber {
let versionComponents = String(SampleSDKVersion).components(
separatedBy: ".")
if versionComponents.count >= 3 {
let majorVersion = Int(versionComponents[0]) ?? 0
let minorVersion = Int(versionComponents[1]) ?? 0
let patchVersion = Int(versionComponents[2]) ?? 0
return GADVersionNumber(
majorVersion: majorVersion, minorVersion: minorVersion, patchVersion: patchVersion)
}
return GADVersionNumber()
}
static func adapterVersion() -> GADVersionNumber {
let versionComponents = String(SampleAdSDK.SampleAdSDKVersionNumber).components(
separatedBy: ".")
var version = GADVersionNumber()
if versionComponents.count == 4 {
version.majorVersion = Int(versionComponents[0]) ?? 0
version.minorVersion = Int(versionComponents[1]) ?? 0
version.patchVersion = Int(versionComponents[2]) * 100 + Int(versionComponents[3])
}
return version
}
Objective-C
+ (GADVersionNumber)adSDKVersion {
NSArray *versionComponents =
[SampleSDKVersion componentsSeparatedByString:@"."];
GADVersionNumber version = {0};
if (versionComponents.count >= 3) {
version.majorVersion = [versionComponents[0] integerValue];
version.minorVersion = [versionComponents[1] integerValue];
version.patchVersion = [versionComponents[2] integerValue];
}
return version;
}
+ (GADVersionNumber)adapterVersion {
NSArray *versionComponents =
[SampleCustomEventAdapterVersion componentsSeparatedByString:@"."];
GADVersionNumber version = {0};
if (versionComponents.count == 4) {
version.majorVersion = [versionComponents[0] integerValue];
version.minorVersion = [versionComponents[1] integerValue];
version.patchVersion = [versionComponents[2] integerValue] * 100 +
[versionComponents[3] integerValue];
}
return version;
}
请求广告
若要请求广告,请参阅针对具体广告格式的说明: