Get iOS device unique ID

Source: Internet
Author: User
Tags unique id uuid


During the development process, we are often asked to obtain a unique indication of each device so that the background can be processed accordingly. Let's look at the ways to get the unique markings on the device and then analyze the pros and cons of these methods.
The specific can be divided into the following kinds:
1. UUID
2, IDFA
3, IDFV
4, MAC
5. UUID



Here we will analyze the pros and cons of each of the methods of acquisition 1, UDID



What is Udid
Udid"unique Device Identifier description" is a serial number of 40 strings of substrings and numbers to distinguish each unique iOS device, including IPhones, IPads, and IPod touches, These codes appear to be random, and are actually associated with the characteristics of the hardware device.



What Udid is used for.
Udid can correlate various other data to related devices. For example, connecting to a developer account allows a device to be installed or tested before it is released, or it can allow developers to experience the iOS beta. Apple uses Udid to connect to Apple IDs, which automatically download and install apps purchased from the App Store, save music purchased from itunes, and help Apple send push notifications and instant messages. In the early days of iOS, Udid was used by third party developers and web advertising businesses to collect user data, and to correlate addresses, record usage habits ... To push the precision ads.



Why Apple opposes the developers using Udid.
After IOS 2.0, Uidevice provides a way to get a unique identifier for a device uniqueidentifier, by which we can get the serial number of the device, which is the only identifier that can be confirmed so far. Many developers associate Udid with the user's real name, password, address, and other data, and the network Snoop collects the data from multiple applications and then gets a lot of privacy data from that person. At the same time, most applications do transmit udid and private information frequently. To avoid class action, Apple finally decided to abolish the practice when it was in iOS 5, where the developer was booted to generate a unique identifier that only detects applications and other information is not available. Application attempts now to acquire Udid have been banned and are not allowed on shelves.



So this method is void. 2, Idfa



Full Name: Advertisingidentifier
Get code:

#import <AdSupport / AdSupport.h>
  NSString * adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
1 2 1 2 Source: iOS6.0 and later Description: Literal translation is the advertising id, all apps on the same device will get the same value, which is specifically designed by Apple for each advertising provider to track users, users can Reset the value of this id in Settings | Privacy | Ad Tracking, or restrict the use of this id, so this id may not get the value, but fortunately Apple allows tracking by default, and ordinary users do n’t know that This setting is basically used to monitor the promotion effect, but it is more than enough. Note: Since idfa will not be available, it can never be used as the main id for business analysis to identify users. 3. IDFV
Full name: identifierForVendor
Get the code:

NSString * idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
1 1 Source: iOS6.0 and later Description: As the name implies, it is used by Vendor to identify users. Each device has the same value in the same Vender application. The Vender refers to the application provider, but to be precise, the first two parts of the reversed BundleID are used for matching. If the same is the same Vender, for example, for com.taobao.app1, com.taobao.app2 BundleID belongs to the same Vender and shares the same idfv value. Different from idfa, the value of idfv can be obtained, so it is very suitable as the main id of internal user behavior analysis to identify users and replace OpenUDID. Note: If the user uninstalls all apps belonging to this Vender, the value of idfv will be reset, that is, reinstall this Vender App, the value of idfv is different from before. 4. MAC address
Use the MAC address of WiFi to replace the uniqueIdentifier method that has been abandoned. Specifically see: http://stackoverflow.com/questions/677530/how-can-i-programmatically-get-the-mac-address-of-an-iphone

However, in iOS 7, Apple once again mercilessly blocked the mac address, and all the mac addresses obtained using the previous method became 02: 00: 00: 00: 00: 00. 5. UUID

We can get the UUID, and then save the UUID in KeyChain.

In this way, even if the APP is deleted and installed again, it can be read back from KeyChain. Using group can also ensure that all programs of the same developer can obtain the same and unchanged UDID for the same device.
But uuid will still change after flashing or reinstalling the system.

Use Keychain to store UUID as password information.
Rough process: Get UUID through AdSupport (reason AdSupport can be cross-application)

 1. (NSString *) appleIFA {
    NSString * ifa = nil;
    Class ASIdentifierManagerClass = NSClassFromString (@ "ASIdentifierManager");
    if (ASIdentifierManagerClass) {// a dynamic way of checking if AdSupport.framework is available
        SEL sharedManagerSelector = NSSelectorFromString (@ "sharedManager");
        id sharedManager = ((id (*) (id, SEL)) [ASIdentifierManagerClass methodForSelector: sharedManagerSelector]) (ASIdentifierManagerClass, sharedManagerSelector);
        SEL advertisingIdentifierSelector = NSSelectorFromString (@ "advertisingIdentifier");
        NSUUID * advertisingIdentifier = ((NSUUID * (*) (id, SEL)) [sharedManager methodForSelector: advertisingIdentifierSelector]) (sharedManager, advertisingIdentifierSelector);
        ifa = [advertisingIdentifier UUIDString];
    }
    return ifa;
}
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 If AdSupport is not supported, then use IFV / IDFV (Identifier for Vendor)
+ (NSString *) appleIFV {
    if (NSClassFromString (@ "UIDevice") && [UIDevice instancesRespondToSelector: @selector (identifierForVendor)]) {
        // only available in iOS> = 6.0
        return [[UIDevice currentDevice] .identifierForVendor UUIDString];
    }
    return nil;
}
1 2 3 4 5 6 7 1 2 3 4 5 6 7 If none of the above are supported, use CFUUIDRef to manually create a UUID
+ (NSString *) randomUUID {
    if (NSClassFromString (@ "NSUUID")) {// only available in iOS> = 6.0
        return [[NSUUID UUID] UUIDString];
    }
    CFUUIDRef uuidRef = CFUUIDCreate (kCFAllocatorDefault);
    CFStringRef cfuuid = CFUUIDCreateString (kCFAllocatorDefault, uuidRef);
    CFRelease (uuidRef);
    NSString * uuid = [((__bridge NSString *) cfuuid) copy];
    CFRelease (cfuuid);
    return uuid;
}
1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 Finally, add to Keychain
+ (void) setValue: (NSString *) value forKey: (NSString *) key inService: (NSString *) service {
    NSMutableDictionary * keychainItem = [[NSMutableDictionary alloc] init];
    keychainItem [(__ bridge id) kSecClass] = (__bridge id) kSecClassGenericPassword;
    keychainItem [(__ bridge id) kSecAttrAccessible] = (__bridge id) kSecAttrAccessibleAlways;
    keychainItem [(__ bridge id) kSecAttrAccount] = key;
    keychainItem [(__ bridge id) kSecAttrService] = service;
    keychainItem [(__ bridge id) kSecValueData] = [value dataUsingEncoding: NSUTF8StringEncoding];
    SecItemAdd ((__ bridge CFDictionaryRef) keychainItem, NULL);
}
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
If the provisioning profile is updated, Keychain data will be lost. So we should backup UUID in NSUserDefault.

[[NSUserDefaults standardUserDefaults] setObject: @ ”123456-1234-1234-12345678” forKey: @ "deviceUID"];
[[NSUserDefaults standardUserDefaults] synchronize];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.