设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助自定义事件,您可以为支持的广告联盟以外的广告联盟添加广告瀑布流中介。为此,您可以为要集成的广告联盟实现自定义事件适配器。
前提条件
您必须先将以下广告格式之一集成到您的应用中,然后才能创建自定义事件:
在界面中创建自定义事件
您必须先在 Ad Manager 界面中创建自定义事件。请参阅创建和管理收益组中的说明。
您需要提供以下信息:
- 类名称
实现自定义事件适配器的类的完全限定名称,例如,SampleCustomEvent
;或者 MediationExample.SampleCustomEventSwift
(如果您的类是用 Swift 实现的)。
如果项目中包含多个目标,或者项目名称与目标名称不一致,则需要包含目标名称。如果包含目标名称,看起来将如下所示:appName_targetName.className
。此外,请务必将短划线等任何非字母数字字符替换为下划线。示例。
- 标签
定义广告来源的唯一名称。
- 参数
传递给自定义事件适配器的可选字符串参数。
若要创建自定义事件,首先要实现 GADMediationAdapter
协议,如我们的示例中的 SampleCustomEvent
类所示。
该类负责接收来自 Ad Manager 的消息和委托创建正确广告格式的责任。
初始化适配器
当 Google 移动广告 SDK 初始化时,系统会针对在 Ad Manager 界面中为应用配置的所有受支持的第三方适配器和自定义事件调用 setUpWithConfiguration:completionHandler:
。您可以使用此方法对自定义事件所需的第三方 SDK 执行任何必要的设置或初始化操作。
Swift
import GoogleMobileAds
class SampleCustomEvent: NSObject, MediationAdapter {
static func setUpWith(
_ configuration: MediationServerConfiguration,
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() -> VersionNumber {
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 VersionNumber(
majorVersion: majorVersion, minorVersion: minorVersion, patchVersion: patchVersion)
}
return VersionNumber()
}
static func adapterVersion() -> VersionNumber {
let versionComponents = String(SampleAdSDK.SampleAdSDKVersionNumber).components(
separatedBy: ".")
var version = VersionNumber()
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;
}
请求广告
若要请求广告,请参阅适用于具体广告格式的操作说明:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[[["\u003cp\u003eCustom events allow you to add waterfall mediation for unsupported ad networks by implementing a custom event adapter.\u003c/p\u003e\n"],["\u003cp\u003eBefore creating custom events, you must integrate an ad format (Banner, Interstitial, Native, or Rewarded) into your app.\u003c/p\u003e\n"],["\u003cp\u003eA custom event needs to be created in the Ad Manager UI, requiring a Class Name, Label, and optional Parameter.\u003c/p\u003e\n"],["\u003cp\u003eYou must implement the \u003ccode\u003eGADMediationAdapter\u003c/code\u003e protocol to receive messages from Ad Manager and delegate ad creation responsibility.\u003c/p\u003e\n"],["\u003cp\u003eCustom events must report both the custom event adapter version and the third-party SDK version to the Google Mobile Ads SDK.\u003c/p\u003e\n"]]],[],null,["Custom events let you add waterfall mediation for an ad network that isn't a\n[supported ad network](/ad-manager/mobile-ads-sdk/ios/choose-networks). You do this by implementing\na custom event adapter for the ad network you want to integrate.\n\nPrerequisites\n\nBefore you can create custom events, you must first integrate one of the\nfollowing ad format into your app:\n\n- [Banner](/ad-manager/mobile-ads-sdk/ios/banner)\n- [Interstitial](/ad-manager/mobile-ads-sdk/ios/interstitial)\n- [Native](/ad-manager/mobile-ads-sdk/ios/native)\n- [Rewarded](/ad-manager/mobile-ads-sdk/ios/rewarded)\n\nCreate a custom event in the UI\n\nA custom event must first be created in the Ad Manager\nUI. See the instructions in\n\n[Create and manage yield\ngroups](//support.google.com/admanager/answer/7390828).\n\n\nYou need to supply the following:\n\nClass Name\n\n: The fully-qualified name of the class that implements the custom event\n adapter---for example,\n\n `SampleCustomEvent`; or if your class is implemented in Swift,\n `MediationExample.SampleCustomEventSwift`.\n\n Target name is required if you have multiple targets in your project or if\n the project name is different from the target name. With the target name, it\n would look like this: `appName_targetName.className`. In addition, remember\n to replace any non-alphanumeric characters such as dashes with underscores.\n [Example](//maximbilan.medium.com/ios-objective-c-project-nsclassfromstring-method-for-swift-classes-dbadb721723).\n\nLabel\n\n: A unique name defining the ad source.\n\nParameter\n\n: An optional string argument passed to your custom event adapter.\n\nImplement GADMediationAdapter\n\nThe first step to creating a custom event is implementing the\n`GADMediationAdapter` protocol as shown by the `SampleCustomEvent` class\nin [our example](//github.com/googleads/googleads-mobile-ios-mediation).\n\nIt is the responsibility of this class to receive messages from\nAd Manager and delegate the responsibility of creating\nthe correct ad format.\n\nInitialize the adapter\n\nWhen Google Mobile Ads SDK initializes,\n\n`setUpWithConfiguration:completionHandler:`\n\nis invoked on all supported third-party adapters and custom events configured\nfor the app within the Ad Manager UI. Use this method to\nperform any necessary setup or initialization on the required third-party SDK\nfor your custom event. \n\nSwift \n\n import GoogleMobileAds\n\n class SampleCustomEvent: NSObject, MediationAdapter {\n\n static func setUpWith(\n _ configuration: MediationServerConfiguration,\n completionHandler: @escaping GADMediationAdapterSetUpCompletionBlock\n ) {\n // This is where you will initialize the SDK that this custom event is built\n // for. Upon finishing the SDK initialization, call the completion handler\n // with success.\n completionHandler(nil)\n }\n }\n\nObjective-C \n\n #import \"SampleCustomEvent.h\"\n\n @implementation SampleCustomEvent\n\n + (void)setUpWithConfiguration:(nonnull GADMediationServerConfiguration *)configuration\n completionHandler:(nonnull GADMediationAdapterSetUpCompletionBlock)completionHandler {\n // This is where you initialize the SDK that this custom event is built\n // for. Upon finishing the SDK initialization, call the completion handler\n // with success.\n completionHandler(nil);\n }\n\nReport version numbers\n\nAll custom events must report to Google Mobile Ads SDK both the version of\nthe custom event adapter itself and the version of the third-party SDK the\ncustom event interfaces with. Versions are reported as\n\n[`GADVersionNumber`](/ad-manager/mobile-ads-sdk/ios/api/reference/Structs/GADVersionNumber)\n\nobjects: \n\nSwift \n\n static func adSDKVersion() -\u003e VersionNumber {\n let versionComponents = String(SampleSDKVersion).components(\n separatedBy: \".\")\n\n if versionComponents.count \u003e= 3 {\n let majorVersion = Int(versionComponents[0]) ?? 0\n let minorVersion = Int(versionComponents[1]) ?? 0\n let patchVersion = Int(versionComponents[2]) ?? 0\n\n return VersionNumber(\n majorVersion: majorVersion, minorVersion: minorVersion, patchVersion: patchVersion)\n }\n\n return VersionNumber()\n }\n\n static func adapterVersion() -\u003e VersionNumber {\n let versionComponents = String(SampleAdSDK.SampleAdSDKVersionNumber).components(\n separatedBy: \".\")\n var version = VersionNumber()\n if versionComponents.count == 4 {\n version.majorVersion = Int(versionComponents[0]) ?? 0\n version.minorVersion = Int(versionComponents[1]) ?? 0\n version.patchVersion = Int(versionComponents[2]) * 100 + Int(versionComponents[3])\n }\n return version\n }\n\nObjective-C \n\n + (GADVersionNumber)adSDKVersion {\n NSArray *versionComponents =\n [SampleSDKVersion componentsSeparatedByString:@\".\"];\n GADVersionNumber version = {0};\n if (versionComponents.count \u003e= 3) {\n version.majorVersion = [versionComponents[0] integerValue];\n version.minorVersion = [versionComponents[1] integerValue];\n version.patchVersion = [versionComponents[2] integerValue];\n }\n return version;\n }\n\n + (GADVersionNumber)adapterVersion {\n NSArray *versionComponents =\n [SampleCustomEventAdapterVersion componentsSeparatedByString:@\".\"];\n GADVersionNumber version = {0};\n if (versionComponents.count == 4) {\n version.majorVersion = [versionComponents[0] integerValue];\n version.minorVersion = [versionComponents[1] integerValue];\n version.patchVersion = [versionComponents[2] integerValue] * 100 +\n [versionComponents[3] integerValue];\n }\n return version;\n }\n\nRequest ad\n\nTo request an ad, refer to the instructions specific to the ad format:\n\n- [Banner](/ad-manager/mobile-ads-sdk/ios/custom-events/banner)\n- [Interstitial](/ad-manager/mobile-ads-sdk/ios/custom-events/interstitial)\n- [Native](/ad-manager/mobile-ads-sdk/ios/custom-events/native)\n- [Rewarded](/ad-manager/mobile-ads-sdk/ios/custom-events/rewarded)"]]