设置

通过自定义事件,您可以为不受支持的广告联盟添加广告瀑布流中介。为此,您可以为要集成的广告联盟实现自定义事件适配器。

您可以在我们的 GitHub 代码库中找到完整的示例自定义事件项目。

前提条件

您必须先将以下广告格式之一集成到您的应用中,然后才能创建自定义事件:

在界面中创建自定义事件

必须先在 AdMob界面中创建自定义事件。请参阅 添加自定义事件中的说明。

您需要提供以下信息:

课程名称

实现自定义事件适配器的类的完全限定名称,例如 SampleCustomEvent;或者,如果您的类是在 Swift 中实现的,则使用 MediationExample.SampleCustomEventSwift

如果项目中有多个目标,或者项目名称与目标名称不同,则必须指定目标名称。如果包含目标名称,看起来将如下所示:appName_targetName.className。此外,请务必将短划线等任何非字母数字字符替换为下划线。示例

标签

定义广告来源的唯一名称。

参数

传递给自定义事件适配器的可选字符串参数。

实现 GADMediationAdapter

若要创建自定义事件,首先要实现 GADMediationAdapter 协议,如我们的示例中的 SampleCustomEvent 类所示。

此类负责接收来自AdMob 的消息,并委派创建正确广告格式的责任。

初始化适配器

当 Google 移动广告 SDK 初始化时,系统会针对在 AdMob 界面中为应用配置的所有受支持的第三方适配器和自定义事件调用 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;
}

请求广告

要请求广告,请参阅广告格式的具体说明: