Jasper Kennis
Jasper Kennis

Reputation: 3062

How to get a "hashed device id" for testing admob on ios

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" ];

Upvotes: 16

Views: 11066

Answers (7)

elbik
elbik

Reputation: 1907

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.

Upvotes: 3

Andrew Duncan
Andrew Duncan

Reputation: 3753

For some reason I was not seeing the "To get test ads..." in the Console. In any case, you might want your beta testers to not have to delve into the darkness to find this and send it to you to hard-code. Here is Felix's (correct, thankyouverymuch) answer in Swift 3 version, using code from http://iosdeveloperzone.com/2014/10/03/using-commoncrypto-in-swift/

Note that you will have to have a bridging header with the following:

#import <CommonCrypto/CommonCrypto.h>

Here's the hash function:

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
}

And here it is put to use in Google's sample code:

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

Upvotes: 2

Jervisbay
Jervisbay

Reputation: 159

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)

Upvotes: 0

Eric Yang
Eric Yang

Reputation: 1022

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.

Upvotes: -1

Felix
Felix

Reputation: 35394

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;

}

Upvotes: 35

Michael Frederick
Michael Frederick

Reputation: 16714

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.

Upvotes: 2

Codo
Codo

Reputation: 78905

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.

Upvotes: 16

Related Questions