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


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;


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.

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

The extension for String class was taken here.

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.

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

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_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 ]

find some log like following

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


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

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]

Need Your Help

Adding double quote delimiters into csv file

excel csv double-quotes

I have a number of text files which contain radio programme titles where each item is on a separate line, e.g.:

JavaScript: Display positive numbers with the plus sign

javascript numbers

How would I display positive number such as 3 as +3 and negative numbers such -5 as -5? So, as follows: