Unique identifiers for iOS devices

Source: Internet
Author: User

1. Disabled-[uidevice uniqueidentifier] Apple always sees the user's privacy as important. -[uidevice uniqueidentifier] was abandoned at the time of IOS5 's actual iOS5, but it was completely disabled in iOS7. Xcode5 won't even allow you to compile apps that contain guidelines to-[uidevice uniqueidentifier]. In addition, the app that used-[uidevice uniqueidentifier] before IOS7 would not return the device's UUID if it ran on iOS7, but instead would return a string of strings, starting with FFFFFFFF, followed by-[uidevice The hexadecimal value of Identifierforvendor]. 2.MAC address can no longer be used to set up a device there is also a way to generate unique identifiers for iOS devices by using the media Access Control (MAC) address of your iOS device. A MAC address is a unique number that is assigned to the network adapter at the physical network level.  At this address, Apple has other names, such as a hardware address or a WiFi address, which means the same thing. Hardware There are many projects and frameworks that use this method to generate a unique device ID. such as Odin. However, Apple does not want someone to tell the user through the MAC address, so if you query the MAC address on the IOS7 system, it will now only return 02:00:00:00:00:00. 3. Now Apple has made it clear that you should use-[uidevice Identifierforvendor] or-[asidentifiermanager advertisingidentifier] as a unique identifier for your frame and app. Frankly speaking, it is not so difficult to deal with these changes, see the following code snippet:
nsstring *identifierforvendor = [[Uidevice Currentdevice].identifierforvendor Uuidstring]; NSString *identifierforadvertising = [[Asidentifiermanager sharedmanager].advertisingidentifier UUIDString]; Each method is adapted to a particular usage: Identifierforvendor is the only value for a vendor, which means that an app issued by the same company will have the same identifier when it runs on the same device.  However, if the user removes the vendor's app and then re-installs it, the identifier will be inconsistent. Advertisingidentifier will return the same value for all software vendors on this device, so it can only be used when advertising. This value can change in many cases, such as when the user initializes the device. 
4. Because Identifierforvendor deleted the vendor's app and then re-install it, this identifier will be inconsistent, so to solve this problem can be the first generation of unique identifiers, saved to keychain, When the app is deleted, the data in the keychain is still there, and the next time you take it from the keychain, OK.
That's the nonsense, here's the code:
 can copy the following code directly to use, change your class name is OK 
#define KEY_UDID @ "Key_udid" #define Key_in_keychain @ "Key_in_keychain" #import <Security/Security.h> #i Mport "APPIdentificationManage.h" @implementation appidentificationmanagesingleton_implementation ( appidentificationmanage) #pragma mark gets uuid/** * This UUID is inside the same program-the same vindor-is not changed under the same device * This UUID is unique but will change after the app is removed and reinstalled. The measures taken are: Only get once saved in the keychain, then get **/-(NSString *) from the keychain openudid{nsstring *identifierforvendor = [[Uidevice currentdevice].id    Entifierforvendor uuidstring]; return Identifierforvendor;} #pragma mark save UUID to Keychain-(void) Saveudid: (NSString *) udid{nsmutabledictionary *udidkvpairs = [Nsmutabledictionary dicti    Onary];    [Udidkvpairs Setobject:udid Forkey:key_udid]; [[Appidentificationmanage Sharedappidentificationmanage] Save:key_in_keychain data:udidkvpairs];}  #pragma mark reads uuid/** * First obtains the UUID from memory, if it is not retrieved from the keychain, if not, a new UUID is generated and saved to the keychain for later use **/-(ID) readudid{if (_uuid = = Nil | | _uuid.length = = 0) {nsmutabledictionary *udidkvpairs = (nsmutabledictionary*) [[Appidentificationmanage sharedappidentificationmanage] load:key_in_keychain];        NSString *uuid = [Udidkvpairs objectforkey:key_udid];            if (uuid = = Nil | | uuid.length = 0) {uuid = [self openudid];        [Self saveudid:uuid];    } _uuid = uuid; } return _uuid;} #pragma mark Delete uuid-(void) deleteuuid{[appidentificationmanage Delete:key_in_keychain];} #pragma mark Query Keychain-(Nsmutabledictionary *) Getkeychainquery: (NSString *) Service {return [nsmutabledictionary Dictionar            Ywithobjectsandkeys: (__bridge_transfer ID) Ksecclassgenericpassword, (__bridge_transfer ID) kSecClass, Service, (__bridge_transfer ID) ksecattrservice, service, (__bridge_transfer ID) ksecattraccount, (__ Bridge_transfer ID) Ksecattraccessibleafterfirstunlock, (__bridge_transfer ID) ksecattraccessible, nil];} #pragma mark saves data to keychain-(void) Save: (NSString *) service data: (ID) Data {nsmutabledictionary *keychainquery = [self getKeychainquery:service];    Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery); [Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (__bridge_transfer ID)    Ksecvaluedata]; Secitemadd ((__bridge_retained cfdictionaryref) keychainquery, NULL);}    #pragma mark load the data in keychain-(ID) Load: (NSString *) Service {ID ret = nil;    Nsmutabledictionary *keychainquery = [self getkeychainquery:service];    [Keychainquery setobject: (ID) kcfbooleantrue Forkey: (__bridge_transfer ID) ksecreturndata];    [Keychainquery setobject: (__bridge_transfer ID) ksecmatchlimitone forkey: (__bridge_transfer ID) kSecMatchLimit];    Cfdataref keyData = NULL; if (secitemcopymatching (__bridge_retained cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = = NOERR) {@t        ry {ret = [nskeyedunarchiver unarchiveobjectwithdata: (__bridge_transfer NSData *) KeyData];        } @catch (NSException *e) {NSLog (@ "Unarchive of%@ failed:%@", service, E); } @finally {}} return ret;} #pragma mark Delete data in keychain-(void) Delete: (NSString *) Service {nsmutabledictionary *keychainquery = [Self getkeychainquery:s    Ervice]; Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery);} @end


Unique identifiers for iOS devices

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.