如何在ios上测试admob的“哈希设备ID”

在实施AdMob时,您可以定义一系列测试ID,以便Google知道向这些设备投放测试广告,而不是真实广告。 但是,它需要“散列设备ID”。 这对我来说似乎有点模糊。 他们在谈论什么ID以及他们希望我使用什么样的哈希方法?

我在谈论应该进入的位:

request.testDevices = @[ @"hashed-device-id" ]; 

我想出了如何生成AdMob设备ID:只需计算advertisingIdentifier的MD5。

 #import  #include  - (NSString *) admobDeviceID { NSUUID* adid = [[ASIdentifierManager sharedManager] advertisingIdentifier]; const char *cStr = [adid.UUIDString UTF8String]; unsigned char digest[16]; CC_MD5( cStr, strlen(cStr), digest ); NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) [output appendFormat:@"%02x", digest[i]]; return output; } 

在不设置测试设备的情况下启动应用程序并查看调试器输出。 你会在那里找到一条消息:

  To get test ads on this device, call: request.testDevices = @[ @"49cd348fa9c01223dd293bcce92f1e08" ]; 

我猜这个消息是自我解释的。

我以这种方式获取设备ID:Swift 3.0

不要忘记将#import 添加到Xcode创建的ObjC-Swift桥接头中。

 extension String { var md5: String! { let str = self.cString(using: String.Encoding.utf8) let strLen = CC_LONG(self.lengthOfBytes(using: String.Encoding.utf8)) let digestLen = Int(CC_MD5_DIGEST_LENGTH) let result = UnsafeMutablePointer.allocate(capacity: digestLen) CC_MD5(str!, strLen, result) let hash = NSMutableString() for i in 0.. 

这里是String类的扩展。

查看ASIdentifierManager 。 它专门用于访问用于投放广告的唯一设备标识符。 您可以获得当前设备的唯一标识符,如下所示:

 ASIdentifierManager *manager = [ASIdentifierManagerClass sharedManager]; NSString *uniqueDeviceId = [[manager advertisingIdentifier] UUIDString]; 

访问设备的唯一标识符的另一种方法是:

 NSString *uniqueDeviceId = [[UIDevice currentDevice] identifierForVendor]; 

但是, 根据Apple的文档 , identifierForVendor不用于广告目的。

出于某种原因,我没有在控制台中看到“获取测试广告……”。 在任何情况下,您可能希望您的测试版测试人员不必钻研黑暗来找到它并将其发送给您进行硬编码。 这是使用来自http://iosdeveloperzone.com/2014/10/03/using-commoncrypto-in-swift/的代码的Swix 3版本中的Felix(正确的,非常多的)答案

请注意,您必须具有以下的桥接标头:

 #import  

这是哈希函数:

 func md5(_ string: String) -> String { let context = UnsafeMutablePointer.allocate(capacity: 1) var digest = Array(repeating:0, count:Int(CC_MD5_DIGEST_LENGTH)) CC_MD5_Init(context) CC_MD5_Update(context, string, CC_LONG(string.lengthOfBytes(using: String.Encoding.utf8))) CC_MD5_Final(&digest, context) context.deallocate(capacity: 1) var hexString = "" for byte in digest { hexString += String(format:"%02x", byte) } return hexString } 

在这里,它在Google的示例代码中使用:

 func createAndLoadInterstitial() { interstitial = GADInterstitial(adUnitID: G.googleMobileTestAdsId) interstitial.delegate = self let request = GADRequest() // Here's the magic. let id = ASIdentifierManager.shared().advertisingIdentifier! let md5id = md5(id.uuidString) // And now we use the magic. request.testDevices = [ kGADSimulatorID, md5id ] interstitial.load(request) } 

找到一些如下的日志

   To get test ads on this device, call: request.testDevices = @[ @"7505289546eeae64cd2fxxxxxa2b94" ]; 

要么

 Use AdRequest.Builder.addTestDevice("AEC1F310D326xxxxx37BC") to get test ads on this device. 

在Swift 2,Xcode 7.3中我做了这个,广告横幅现在显示我在模拟器中运行的测试广告:

  let request = GADRequest() request.testDevices = [kGADSimulatorID] bannerView.loadRequest(request)