识别 Admob 的测试设备

Identify the test device for Admob

我使用下面的代码:

let request : GADRequest = GADRequest ()

request.testDevices = ["xxxxxxx",kGADSimulatorID]

但我收到以下警告:

'testDevices' is deprecated: Use GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers.

我是否使用语法来删除警告?

使用语法删除警告:

GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers

你根本不需要设置这个。正如 AdMob 开发人员文档所说:

iOS simulators are automatically configured as test devices.

来源:https://developers.google.com/admob/ios/test-ads#enable_test_devices

你应该使用这个:

GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = ["YOUR IDENTIFIER PRINTED ON DEBUGGER"]

而不是:

request.testDevices = ["YOUR IDENTIFIER PRINTED ON DEBUGGER"]

如果您使用的是情节提要,则在情节提要中创建视图并分配横幅class,这样您的广告就可以正常投放了。

Swift 解决方案

原来AdMob/GoogleAdManagerdeviceId可以通过计算MD5 of the advertisingIdentifier得到。这样您就可以在代码中检索和使用测试 deviceId,而无需事先从控制台日志中获取设备标识符。

为了避免使用 ObjC-Swift 桥接头(通过 <CommonCrypto/CommonCrypto.h> 获取 MD5),我建议在 [=16= 周围使用 Swift 包装器] 框架,例如这个:

使用上面的框架(它向 String 添加了一个扩展 属性 来计算 MD5 散列),这是一个简单的查询问题:

GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers ?? []).contains(ASIdentifierManager.shared().advertisingIdentifier.uuidString.md5)

这是对10623169答案的修改。

要获取当前设备的有效 ID,可以在“testDevices”中设置,获取此的 MD5UIDevice.current.identifierForVendor?.uuidString

如果用户未授予跟踪权限,asIdentifier 值可能完全无效。但是 UIDevice.current.identifierForVendor 是该应用程序的有效 UUID,它将在整个启动过程中持续存在(但如果您删除该应用程序并重新安装,则可能不会)。