安全信号是客户端设备收集并与选定出价方分享的编码数据。本页介绍了如何使用互动式媒体广告 (IMA) SDK 收集安全信号并将其发送到 Google Ad Manager。
准备工作
在继续之前,请验证您是否拥有适用于 iOS 的 IMA SDK v3.18.5 或更高版本。
创建安全信号适配器接口
如需收集和提供安全信号,请创建实现以下接口的类:
Objective-C
- MySecureSignalsAdapter.h:
#import <Foundation/Foundation.h>
#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>
/** An example implementation of Secure Signals adapter. */
@interface MySecureSignalsAdapter : NSObject <IMASecureSignalsAdapter>
@end
- MySecureSignalsAdapter.m:
@implementation MySecureSignalsAdapter
/**
* Default constructor with no arguments for IMA SDK to instantiate this class.
*/
- (instancetype)init {
self = [super init];
return self;
}
@end
Swift
import Foundation
import GoogleInteractiveMediaAds
/** An example implementation of Secure Signals adapter. */
@objc(MySecureSignalsAdapter)
public class MySecureSignalsAdapter: IMASecureSignalsAdapter {
/**
* Default constructor with no arguments for IMA SDK to instantiate this class.
*/
override init() {
super.init()
}
}
初始化适配器
IMA SDK 通过调用适配器的初始化方法来初始化每个适配器一次。实现此方法以开始任何加密依赖项、设置缓存或预先计算在所有信号收集调用中保持不变的任何信号。
以下示例初始化了适配器:
Objective-C
...
@interface MySecureSignalsAdapter
@property(nonatomic) NSError *initError;
@end
...
/**
* Initialize your SDK and any dependencies.
* IMA SDK calls this function exactly once before signal collection.
*/
- (instancetype)init {
self = [super init];
@try {
// Initialize your SDK and any dependencies.
...
}
@catch(NSException *exception) {
// Build NSError to be passed by Signal Collector.
_initError = ...;
}
return self;
}
...
Swift
...
@objc(MySecureSignalsAdapter)
public class MySecureSignalsAdapter: IMASecureSignalsAdapter {
...
private var initError
override init() {
super.init()
do {
// Initialize your SDK and any dependencies.
...
} catch {
// Build NSError to be passed by Signal Collector.
self.initError = ...;
}
}
}
信号收集
在发起广告请求之前,IMA SDK 会异步调用一个收集信号的方法。这些信号收集器方法包含一个回调函数,用于传递加密信号或报告错误。
以下示例通过回调函数收集安全信号:
Objective-C
...
/**
* Invokes your SDK to collect, encrypt and pass the signal collection results to IMA SDK.
* IMA SDK calls this function before each ad request.
*
* @param completion A callback function to pass signal collection results to IMA SDK.
*/
- (void)collectSignalsWithCompletion:(IMASignalCompletionHandler)completion {
// Output any initialization errors
if (self.initError) {
completion(nil, self.initError);
return;
}
@try {
// Collect and encrypt the signals.
NSString *signals = ...
// Pass the encrypted signals to IMA SDK.
completion(signals, nil);
}
@catch(NSException *exception) {
NSError *collectSignalError = ...;
// Pass signal collection failures to IMA SDK.
completion(nil, collectSignalError);
}
}
...
Swift
...
/**
* Invokes your SDK to collect, encrypt and pass the signal collection results to IMA SDK.
* IMA SDK calls this function before each ad request.
*
* @param completion A callback function to pass signal collection results to IMA SDK.
*/
public func collectSignals(completion: @escaping IMASignalCompletionHandler) {
if (self.initError) {
completion(nil, self.initError)
return
}
do {
// Collect and encrypt the signals.
var signals = ...
// Pass the encrypted signals to IMA SDK.
completion(signals, nil)
} catch {
NSError collectSignalError = ...
// Pass signal collection failures to IMA SDK.
completion(nil, collectSignalError)
}
}
...
报告错误
为了与使用您的适配器类的用户进行通信,请报告信号收集期间的所有错误,并将这些错误传递给信号收集器回调。此流程用于排查将适配器集成到应用期间出现的问题。
可能出现的错误如下:
- 在应用中找不到您的 SDK 或依赖项。
- 您的 SDK 或依赖项没有正常运行所需的权限或用户同意。
指定适配器版本
在工作流中,验证您是否指定了适配器的版本。IMA SDK 会在每个广告请求中包含您的适配器版本,并在出价请求中通过安全信号传递这些信息。
在出价请求中,您可以根据适配器版本确定适配器用于创建安全信号的加密、编码和格式设置详细信息。
以下示例指定了适配器版本:
Objective-C
...
/**
* Specifies this adapter's version.
*/
static NSInteger const VersionMajor = 1;
static NSInteger const VersionMinor = 0;
static NSInteger const VersionPatch = 1;
...
/**
* @return The version of this adapter.
* IMA SDK calls this function before each ad request.
*/
+ (IMAVersion *)adapterVersion {
// The version of the SecureSignals Adapter.
IMAVersion *adapterVersion = [[IMAVersion alloc] init];
adapterVersion.majorVersion = VersionMajor;
adapterVersion.minorVersion = VersionMinor;
adapterVersion.patchVersion = VersionPatch;
return adapterVersion;
}
...
Swift
...
/**
* Specifies this adapter's version.
*/
static let VersionMajor = 1;
static let VersionMinor = 0;
static let VersionPatch = 1;
...
/**
* @return The version of this adapter.
* IMA SDK calls this function before each ad request.
*/
public static func adapterVersion() -> IMAVersion {
let adapterVersion = IMAVersion()
adapterVersion.majorVersion = self.VersionMajor
adapterVersion.minorVersion = self.VersionMinor
adapterVersion.patchVersion = self.VersionPatch
return adapterVersion
}
...
返回 SDK 运行时版本
您可以设计适配器,使其能够与 SDK 的多个版本搭配使用。为了使适配器能够与多个版本搭配使用,请验证您是否返回了 SDK 的运行时版本。在每项广告请求中,IMA SDK 都会包含运行时版本和适配器版本。
以下示例请求并返回 SDK 运行时版本:
Objective-C
...
/**
* @return The version of your SDK that this adapter is depending on.
* IMA SDK calls this function before each ad request.
*/
+ (IMAVersion *)adSDKVersion {
// Request the version from your SDK and convert to an IMAVersion.
int mySDKVersion[3] = ...
IMAVersion *adSDKVersion = [[IMAVersion alloc] init];
adSDKVersion.majorVersion = mySDKVersion[0];
adSDKVersion.minorVersion = mySDKVersion[1];
adSDKVersion.patchVersion = mySDKVersion[2];
return adSDKVersion;
}
...
Swift
...
/**
* @return The version of your SDK that this adapter is depending on.
* IMA SDK calls this function before each ad request.
*/
public static func adSDKVersion() -> IMAVersion {
// Request the version from your SDK and convert to an IMAVersion.
let mySDKVersion = ...
let adSDKVersion = IMAVersion()
adSDKVersion.majorVersion = mySDKVersion[0]
adSDKVersion.minorVersion = mySDKVersion[1]
adSDKVersion.patchVersion = mySDKVersion[2]
return adSDKVersion
}
...
向 Google 注册适配器
为了让 Google 授权该适配器进行信号收集,您必须向 Google 注册该 iOS 类名称。IMA SDK 只会初始化您向 Google 注册的那些适配器。
验证适配器
如需验证适配器,请完成以下部分:
配置测试应用
在验证适配器之前,请配置测试应用。完成以下步骤:
将 IMA SDK 添加到您的 pod 文件中:
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '10' target "BasicExample" do pod 'GoogleAds-IMA-iOS-SDK', '~> 3.17.0' end
使用 CocoaPods 安装 IMA SDK。如需了解如何通过 CocoaPods 安装 IMA SDK,请参阅使用入门。
在您的 XCode 项目中,添加您的适配器、SDK 和您添加的任何剩余 build 依赖项。
验证信号
如需验证安全信号的长度、加密值、适配器版本和 SDK 版本,请联系支持团队以分享捕获的流量日志。
查看完整示例
本部分包含所有步骤的完整示例,供您参考。
Objective-C
- MySecureSignalsAdapter.h:
#import <Foundation/Foundation.h>
#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h>
/** An example implementation of Secure Signals adapter. */
@interface MySecureSignalsAdapter : NSObject <IMASecureSignalsAdapter>
@end
- MySecureSignalsAdapter.m:
#import "path/to/MyExampleSecureSignalsAdapter.h"
@interface MySecureSignalsAdapter
@property(nonatomic) NSError *initError;
@end
static NSInteger const VersionMajor = 1;
static NSInteger const VersionMinor = 0;
static NSInteger const VersionPatch = 1;
@implementation MySecureSignalsAdapter
/**
* Initialize your SDK and any dependencies.
* IMA SDK calls this function exactly once before signal collection.
*/
- (instancetype)init {
self = [super init];
@try {
// Initialize your SDK and any dependencies.
...
}
@catch(NSException *exception) {
// Build NSError to be passed by Signal Collector.
_initError = ...;
}
return self;
}
/**
* Invokes your SDK to collect, encrypt and pass the signal collection results to IMA SDK.
* IMA SDK calls this function before each ad request.
*
* @param completion A callback function to pass signal collection results to IMA SDK.
*/
- (void)collectSignalsWithCompletion:(IMASignalCompletionHandler)completion {
if (self.initError) {
completion(nil, self.initError);
return;
}
@try {
// Collect and encrypt the signals.
NSString *signals = ...
// Pass the encrypted signals to IMA SDK.
completion(signals, nil);
}
@catch(NSException *exception) {
NSError *collectSignalError = ...;
// Pass signal collection failures to IMA SDK.
completion(nil, collectSignalError);
}
}
/**
* @return The version of this adapter.
* IMA SDK calls this function before each ad request.
*/
+ (IMAVersion *)adapterVersion {
// The version of the SecureSignals Adapter.
IMAVersion *adapterVersion = [[IMAVersion alloc] init];
adapterVersion.majorVersion = VersionMajor;
adapterVersion.minorVersion = VersionMinor;
adapterVersion.patchVersion = VersionPatch;
return adapterVersion;
}
/**
* @return The version of your SDK that this adapter depends on.
* IMA SDK calls this function before each ad request.
*/
+ (IMAVersion *)adSDKVersion {
// Request the version from your SDK and convert to an IMAVersion.
int mySDKVersion[3] = ...
IMAVersion *adSDKVersion = [[IMAVersion alloc] init];
adSDKVersion.majorVersion = mySDKVersion[0];
adSDKVersion.minorVersion = mySDKVersion[1];
adSDKVersion.patchVersion = mySDKVersion[2];
return adSDKVersion;
}
@end
Swift
@objc(MySecureSignalsAdapter)
public class MySecureSignalsAdapter: IMASecureSignalsAdapter {
static let VersionMajor = 1;
static let VersionMinor = 0;
static let VersionPatch = 1;
private var initError
override init() {
super.init()
do {
// Initialize your SDK and any dependencies.
...
} catch {
// Build NSError to be passed by Signal Collector.
self.initError = ...;
}
}
public func collectSignals(completion: @escaping IMASignalCompletionHandler) {
if (self.initError) {
completion(nil, self.initError)
return
}
do {
// Collect and encrypt the signals.
var signals = ...
// Pass the encrypted signals to IMA SDK.
completion(signals, nil)
} catch {
NSError collectSignalError = ...
// Pass signal collection failures to IMA SDK.
completion(nil, collectSignalError)
}
}
public static func adapterVersion() -> IMAVersion {
let adapterVersion = IMAVersion()
adapterVersion.majorVersion = self.VersionMajor
adapterVersion.minorVersion = self.VersionMinor
adapterVersion.patchVersion = self.VersionPatch
return adapterVersion
}
public static func adSDKVersion() -> IMAVersion {
// Request the version from your SDK and convert to an IMAVersion.
let mySDKVersion = ...
let adSDKVersion = IMAVersion()
adSDKVersion.majorVersion = mySDKVersion[0]
adSDKVersion.minorVersion = mySDKVersion[1]
adSDKVersion.patchVersion = mySDKVersion[2]
return adSDKVersion
}
}