[英]Identify the test device for Admob
我使用下面的代碼:
let request: GADRequest = GADRequest ()
request.testDevices = ["xxxxxxx",kGADSimulatorID]
但我收到以下警告:
'testDevices' 已棄用:使用 GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers。
我是否使用語法來刪除警告?
你應該使用這個:
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = ["YOUR IDENTIFIER PRINTED ON DEBUGGER"]
代替:
request.testDevices = ["YOUR IDENTIFIER PRINTED ON DEBUGGER"]
原來AdMob
/ GoogleAdManager
deviceId
可以通過計算adsIdentifier的MD5找到。 通過這種方式,您可以在代碼中檢索和使用測試deviceId
,而無需事先從控制台日志中獲取設備標識符。
為了避免使用 ObjC-Swift 橋接 header (通過<CommonCrypto/CommonCrypto.h>
獲取 MD5),我建議在CommonCrypto
框架周圍使用 Swift 包裝器,例如這個:
使用上述框架(它為String
添加了一個擴展屬性以計算 MD5 哈希),查詢以下內容很簡單:
GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers ?? []).contains(ASIdentifierManager.shared().advertisingIdentifier.uuidString.md5)
使用語法刪除警告:
GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers
您根本不必設置它。 正如 AdMob 開發人員文檔所說:
iOS 模擬器自動配置為測試設備。
來源: https://developers.google.com/admob/ios/test-ads#enable_test_devices
這是對10623169答案的修改。
要獲取當前設備的有效 ID,可以在“testDevices”中設置,獲取以下MD5 : UIDevice.current.identifierForVendor?.uuidString
如果用戶未授予跟蹤權限,則asIdentifier
值可能完全無效。 但是UIDevice.current.identifierForVendor
是應用程序的有效 UUID,它將在啟動期間持續存在(但如果您刪除應用程序並重新安裝,則可能不會)。
如果您使用的是 storyboard,然后在 storyboard 中創建視圖並分配橫幅 class,那么您的廣告就可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.