iOS programming--A unique identifier for iOS devices (OC version) via UUID and keychain instead of MAC address

Source: Internet
Author: User

iOS programming-use UUID and keychain instead of MAC address to implement unique iOS device identifiers (OC version)

Many applications require a unique indication of the phone and require that the unique label not be changed by the app's uninstall or change.

This function can be achieved by MAC address before iOS7, but IOS7 (included) will not be able to get MAC address later; Apple's official recommendation is to use the UUID, but each time with the app unloading load, the UUID changes, then how to deal with it?

We need a value that will not change after the app is unloaded, and keychain happens to do it. With the UUID can be achieved! Let's analyze the following:

1. We first need to import security.framework (keychain relies on it), then need a keychain manager, a UUID manager, the file consists of the following:

2. First of all, the Mykeychainmanager, in fact, is to increase, delete, change and check the keychain, similar to the processing of the database.

First, through the. h file to open the next increase, delete, change, check four interfaces:

#import <Foundation/Foundation.h>@interface  mykeychainmanager:nsobject+ ( Nsmutabledictionary *) Getkeychainquery: (NSString *) service; + (void) Save: (NSString *) service data: (ID) data; + (ID) Load: (NSString *) service; + (void) Delete: (NSString *) service;     @end

. m file implementation interface, keychain use a lot of online, directly paste code:

#import "MyKeyChainManager.h"@implementationMykeychainmanager:nsobject+ (Nsmutabledictionary *) Getkeychainquery: (NSString *) Service {return[nsmutabledictionary Dictionarywithobjectsandkeys: (__bridge_transferID) Ksecclassgenericpassword, (__bridge_transferID) Ksecclass, service, (__bridge_transferID) Ksecattrservice, service, (__bridge_transferID) Ksecattraccount, (__bridge_transferID) Ksecattraccessibleafterfirstunlock, (__bridge_transferID) ksecattraccessible, nil];}+ (void) Save: (NSString *) Service data: (ID) Data {//Get Search DictionaryNsmutabledictionary *keychainquery =[self getkeychainquery:service]; //Delete old item before add New itemSecitemdelete ((__bridge_retained cfdictionaryref) keychainquery); //Add New object to search dictionary (attention:the data format)[Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (__bridge_transferID) Ksecvaluedata]; //ADD item to keychain with the search dictionarySecitemadd ((__bridge_retained cfdictionaryref) keychainquery, NULL);}+ (ID) Load: (NSString *) Service {IDRET =Nil; Nsmutabledictionary*keychainquery =[self getkeychainquery:service]; //Configure the search setting[Keychainquery SetObject: (ID) kcfbooleantrue Forkey: (__bridge_transferID) Ksecreturndata]; [Keychainquery setobject: (__bridge_transferID) Ksecmatchlimitone Forkey: (__bridge_transferID) Ksecmatchlimit]; Cfdataref KeyData=NULL; if(Secitemcopymatching (__bridge_retained cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = =NOERR) {        @try{ret= [Nskeyedunarchiver unarchiveobjectwithdata: (__bridge_transfer NSData *) KeyData]; } @catch(NSException *e) {NSLog (@"unarchive of%@ failed:%@", service, E); } @finally {        }    }    returnret;}+ (void) Delete: (NSString *) Service {nsmutabledictionary*keychainquery =[self getkeychainquery:service]; Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery);}@end


3. Take a look at the Myuuidmanager file to achieve the increase, deletion, modification, and investigation of the UUID, where save is both increased and changed:

#import <Foundation/Foundation.h>@interface  myuuidmanager:nsobject+ (void ) Saveuuid: (NSString *) uuid; + (NSString *) Getuuid; + (void) deleteuuid; @end


. m file to implement it:

#import "MyUUIDManager.h"#import "MyKeyChainManager.h"@implementationMyuuidmanagerStaticNSString *ConstKey_in_keychain =@"Com.myuuid.uuid";+(void) Saveuuid: (NSString *) uuid{if(UUID && uuid.length >0) {[Mykeychainmanager save:key_in_keychain data:uuid]; }}+ (NSString *) getuuid{//first get the UUID field inside the keychain to see if there is anyNSString *uuid = (NSString *) [Mykeychainmanager Load:key_in_keychain]; //if not present, gets the UUID for the first time, so get the save.     if(!uuid | | uuid.length = =0) {cfuuidref Puuid=cfuuidcreate (nil); Cfstringref uuidstring=cfuuidcreatestring (nil, puuid); UUID= [NSString stringWithFormat:@"%@", uuidstring];                [Self saveuuid:uuid];                Cfrelease (PUUID);    Cfrelease (uuidstring); }        returnuuid;}+(void) deleteuuid{[Mykeychainmanager delete:key_in_keychain];}@end


4. Test:

NSString *uuid = [Myuuidmanager getuuid];    NSLog (@ "uuid:%@", uuid); £ º07.641 mytest[3190:  220331] uuid:839e055b-09a5-42e1-a46c-df4481e23333


5. Remove the app and reinstall it, and then print it out:

---£º37.122 mytest[3214:222053 ] uuid:839e055b-09a5-42e1-a46c-df4481e23333

iOS programming--A unique identifier for iOS devices (OC version) via UUID and keychain instead of MAC address

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.