Membuat adaptor sinyal aman

Sinyal aman adalah data yang dienkode yang dikumpulkan dan dibagikan oleh perangkat klien kepada bidder tertentu. Halaman ini memandu Anda dalam mengumpulkan dan mengirim sinyal aman ke Google Ad Manager menggunakan Interactive Media Ads (IMA) SDK.

Sebelum memulai

Sebelum melanjutkan, pastikan Anda memiliki IMA SDK untuk iOS v3.18.5 atau yang lebih tinggi.

Membuat antarmuka adaptor sinyal aman

Untuk mengumpulkan dan memberikan sinyal aman, buat class yang menerapkan antarmuka:

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()
  }
}

Melakukan inisialisasi adaptor

IMA SDK menginisialisasi setiap adaptor satu kali dengan memanggil metode inisialisasi adaptor. Terapkan metode ini untuk memulai dependensi enkripsi, menyiapkan cache, atau menghitung sebelumnya sinyal yang tetap sama di semua panggilan pengumpulan sinyal.

Contoh berikut menginisialisasi adaptor:

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 = ...;
    }
  }
}

Pengumpulan sinyal

Sebelum permintaan iklan dimulai, IMA SDK memanggil metode pengumpulan sinyal secara asinkron. Metode pengumpulan sinyal ini berisi fungsi callback untuk meneruskan sinyal terenkripsi atau melaporkan error.

Contoh berikut mengumpulkan sinyal aman melalui fungsi callback:

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)
    }
  }
...

Melaporkan error

Untuk berkomunikasi dengan pengguna yang menggunakan class adaptor Anda, laporkan semua error selama pengumpulan sinyal dan teruskan ke callback pengumpulan sinyal. Proses ini memecahkan masalah yang terjadi selama integrasi adaptor ke aplikasi.

Error yang mungkin muncul adalah sebagai berikut:

  • SDK atau dependensi Anda tidak ditemukan di aplikasi.
  • SDK atau dependensi Anda tidak memiliki izin atau izin pengguna yang diperlukan agar dapat berfungsi.

Menentukan versi adaptor

Dalam alur kerja, pastikan Anda menetapkan versi adaptor. IMA SDK menyertakan versi adaptor Anda di setiap permintaan iklan dan meneruskannya dengan sinyal aman dalam permintaan bid.

Dalam permintaan bid, berdasarkan versi adaptor, Anda dapat mengidentifikasi detail enkripsi, encoding, dan pemformatan yang digunakan adaptor untuk membuat sinyal aman.

Contoh berikut menentukan versi adaptor:

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
  }
...

Menampilkan versi runtime SDK

Anda dapat mendesain adaptor agar berfungsi dengan beberapa versi SDK. Agar adaptor dapat berfungsi dengan beberapa versi, pastikan Anda menampilkan versi runtime SDK. Dalam setiap permintaan iklan, IMA SDK menyertakan versi runtime dengan versi adaptor.

Contoh berikut meminta dan menampilkan versi runtime 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
  }
...

Daftarkan adaptor ke Google

Agar Google dapat mengizinkan adaptor pengumpulan sinyal, Anda harus mendaftarkan nama class iOS ke Google. IMA SDK hanya melakukan inisialisasi adaptor yang Anda daftarkan dengan Google.

Memvalidasi adaptor

Untuk memvalidasi adaptor, selesaikan bagian berikut:

Mengonfigurasi aplikasi pengujian

Sebelum memvalidasi adaptor, konfigurasikan aplikasi pengujian. Selesaikan langkah-langkah berikut:

  1. Tambahkan IMA SDK ke file pod Anda:

    source 'https://github.com/CocoaPods/Specs.git'
    platform :ios, '10'
    target "BasicExample" do
      pod 'GoogleAds-IMA-iOS-SDK', '~> 3.17.0'
    end
    
  2. Instal IMA SDK menggunakan CocoaPods. Untuk mendapatkan petunjuk tentang cara menginstal IMA SDK melalui CocoaPods, lihat Mulai.

  3. Dalam project XCode, tambahkan adaptor, SDK, dan sisa dependensi build yang telah ditambahkan.

Memverifikasi sinyal

Untuk memverifikasi durasi sinyal aman, nilai terenkripsi, versi adaptor, dan versi SDK, hubungi dukungan untuk membagikan log traffic yang diambil.

Meninjau contoh lengkap

Bagian ini menampilkan contoh lengkap dari semua langkah dan tersedia untuk referensi Anda.

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
  }
}