How to get a “hashed device id” for testing admob

2019-01-23 05:44发布

When implementing AdMob you can define an array of test IDs so that Google knows to serve test ads to these devices, instead of real ads. However, it requires "hashed device IDs". This seems a little vague to me. What ID are they talking about and what hashing method do they expect me to use?

I'm talking about the bit that should go in here:

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

7条回答
smile是对你的礼貌
2楼-- · 2019-01-23 06:16

I figured out how to generate the AdMob device ID: Just compute the MD5 of the advertisingIdentifier.

#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;

}
查看更多
Luminary・发光体
3楼-- · 2019-01-23 06:16

In Swift 2, Xcode 7.3 I did this and the ad banner now shows test ad when I run in simulator:

    let request = GADRequest()
    request.testDevices = [kGADSimulatorID]
    bannerView.loadRequest(request)
查看更多
Emotional °昔
4楼-- · 2019-01-23 06:17

Start the app without setting the test devices and have a look at the debugger output. There you'll find a message like:

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

I guess the message is self explaining.

查看更多
Rolldiameter
5楼-- · 2019-01-23 06:20

Check out ASIdentifierManager. It is created specifically for accessing unique device identifiers to be used for serving ads. You can get a unique identifier for the current device like so:

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

The alternative way to access the unique identifier for a device is:

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

However, as per Apple's documentation, identifierForVendor is not intended to be used for advertising purposes.

查看更多
冷血范
6楼-- · 2019-01-23 06:20

find some log like following

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

or

Use AdRequest.Builder.addTestDevice("AEC1F310D326xxxxx37BC") to get test ads on this device.
查看更多
Fickle 薄情
7楼-- · 2019-01-23 06:27

I get the device id in such way: Swift 3.0

Don't forget to add #import <CommonCrypto/CommonCrypto.h> to the ObjC-Swift bridging header that Xcode creates.

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

The extension for String class was taken here.

查看更多
登录 后发表回答