安全信号是客户端设备收集并与特定出价方共享的经过编码的数据。本页将指导您使用互动式媒体广告 (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
}
}