识别 Admob 的测试设备

问题描述 投票:0回答:6

我使用下面的代码:

let request : GADRequest = GADRequest ()

request.testDevices = ["xxxxxxx",kGADSimulatorID]

但我收到以下警告:

“testDevices”已弃用:使用 GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers。

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

swift admob
6个回答
15
投票

你应该使用这个:

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

代替:

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

3
投票

快速解决方案

事实证明,

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

为了避免使用 ObjC-Swift 桥接标头(通过

<CommonCrypto/CommonCrypto.h>
获取 MD5),我建议在
CommonCrypto
框架周围使用 Swift 包装器,例如这个:

使用上述框架(向

String
添加扩展属性来计算 MD5 哈希),查询非常简单:

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

2
投票

这是对10623169答案的修改。

要获取当前设备的有效 ID(可以在“testDevices”中设置),请获取以下内容的 MD5

UIDevice.current.identifierForVendor?.uuidString

如果用户未授予跟踪权限,则

asIdentifier
值可能完全无效。但
UIDevice.current.identifierForVendor
是应用程序的有效 UUID,它将在启动后持续存在(但如果您删除应用程序并重新安装,则可能不会)。


1
投票

使用语法删除警告:

GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers

1
投票

您根本不必设置此项。正如 AdMob 开发人员文档所说:

iOS 模拟器会自动配置为测试设备。

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


0
投票

快速解决方案

适用于禁用的应用程序跟踪。没有任何外部依赖。

import CryptoKit


var deviceId = ASIdentifierManager.shared().advertisingIdentifier.uuidString
if deviceId == UUID(0).uuidString {
  deviceId = UIDevice.current.identifierForVendor?.uuidString ?? ""
}

let md5 = Insecure.MD5.hash(data: Data(deviceId.utf8))
    .map { String(format: "%02hhx", $0) }.joined()
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = [ md5 ]
© www.soinside.com 2019 - 2024. All rights reserved.