【问题标题】:How to get a "hashed device id" for testing admob on ios如何获取“散列设备 ID”以在 ios 上测试 admob
【发布时间】:2014-07-15 13:55:17
【问题描述】:

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

我说的是应该进入这里的部分:

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

【问题讨论】:

    标签: ios objective-c hash admob


    【解决方案1】:

    我想出了如何生成 AdMob 设备 ID: 只需计算广告标识符的 MD5。

    #import <AdSupport/ASIdentifierManager.h>
    #include <CommonCrypto/CommonDigest.h>
    
    - (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;
    
    }
    

    【讨论】:

    • 哈,太好了!会给你 100 赏金,你错过了不到一个小时。
    • 非常好的答案。完美的解决方案。 :-)
    • @phix23 我正在为 FBAudienceNetwork 尝试同样的事情,但它不起作用。知道我必须做出哪些改变吗?
    • 我不得不使用 NSString *UDIDString = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 而不是广告标识符。至少在我的设备上禁用了广告跟踪。
    【解决方案2】:

    在不设置测试设备的情况下启动应用并查看调​​试器输出。在那里你会发现这样的消息:

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

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

    【讨论】:

    • 是的,我知道,但是我必须连接所有测试用户的设备以获取他们的 id,并且由于其中一些用户不在我的国家/地区,这就造成了问题。所以我一直在寻找一种即时生成哈希的方法。
    【解决方案3】:

    我以这种方式获取设备 ID: 斯威夫特 3.0

    不要忘记将#import &lt;CommonCrypto/CommonCrypto.h&gt; 添加到 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<CUnsignedChar>.allocate(capacity: digestLen)
    
            CC_MD5(str!, strLen, result)
    
            let hash = NSMutableString()
            for i in 0..<digestLen {
                hash.appendFormat("%02x", result[i])
            }
    
            result.deallocate(capacity: digestLen)
    
            return String(format: hash as String)
        }
    }
    
    import AdSupport
    ...
    {
      var uuid: UUID = ASIdentifierManager.shared().advertisingIdentifier
      print("\(uuid.uuidString.md5)")
    }
    

    String 类的扩展名是here

    【讨论】:

      【解决方案4】:

      查看ASIdentifierManager。它是专门为访问用于投放广告的唯一设备标识符而创建的。您可以像这样获取当前设备的唯一标识符:

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

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

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

      但是,as per Apple's documentationidentifierForVendor 不打算用于广告目的。

      【讨论】:

      • 是的,我也做到了。只有谷歌似乎对这个字符串进行了哈希处理,因为我无法让它返回任何格式如49cd348fa9c01223dd293bcce92f1e08 的东西。所以我对如何散列它特别感兴趣。
      【解决方案5】:

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

      请注意,您必须有一个包含以下内容的桥接头:

      #import <CommonCrypto/CommonCrypto.h>
      

      这是哈希函数:

      func md5(_ string: String) -> String {
          let context = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
          var digest = Array<UInt8>(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
      }
      

      这里是在谷歌的示例代码中使用的:

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

      【讨论】:

      • 完美答案!救了我。谢谢!一个问题,我应该在应用上架之前移除测试设备吗?
      • 我在代码中看到我确实删除了发布应用程序的测试设备,但我认为这没有必要。我认为它们对于发布应用程序来说是无操作的。但我找不到我在哪里读到的。在某处的互联网上。
      【解决方案6】:

      在 Swift 2 和 Xcode 7.3 中我这样做了,当我在模拟器中运行时,广告横幅现在会显示测试广告:

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

      【讨论】:

        【解决方案7】:

        找到一些日志,如下所示

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

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

        【讨论】:

          猜你喜欢
          • 2016-04-19
          • 1970-01-01
          • 2011-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-25
          相关资源
          最近更新 更多