To: summary and best practices for how iOS acquires unique identities for devices
Reference: "Get unique ID for iOS device"
To summarize:
1. The code uses the Cfuuid+keychain implementation method.
2.CFUUID, IDFA, IDFV are variable (other methods, such as Apple's own UUID and MAC address are basically banned), but IDFA and IDFV not necessarily be able to obtain (iOS system different version does not necessarily support), so directly selected Cfuuid ( IOS2.0 began to support it).
Ps:
3. Use stringbyreplacingoccurrencesofstring in the code to remove the spacer-the original is this: cae1cc3a-92c1-45a5-9f55-932264020358, Removed and then changed to cae1cc3a92c145a59f55932264020358).
4.SDKMGR is the custom class name and modifies it as needed.
NSString *ConstKey_udid_instead =@"com.app.uuid.test";+ (NSString *) getdeviceuuid{NSString*getudidinkeychain = (NSString *) [Sdkmgr Load:key_udid_instead]; if(!getudidinkeychain | | [Getudidinkeychain isequaltostring:@""]|| [Getudidinkeychain Iskindofclass:[nsnullclass]]) {cfuuidref Puuid=cfuuidcreate (nil); Cfstringref uuidstring=cfuuidcreatestring (nil, puuid); NSString* result = (NSString *) Cfbridgingrelease (Cfstringcreatecopy (NULL, uuidstring)); Cfrelease (PUUID); Cfrelease (uuidstring); //remove the horizontal line and keep the 32-bit lengthNSString *rst = [result stringbyreplacingoccurrencesofstring:@"-"Withstring:@""]; [Sdkmgr Save:key_udid_instead Data:rst]; Getudidinkeychain= (NSString *) [Sdkmgr Load:key_udid_instead]; } returnGetudidinkeychain;}#pragmaMark-private+ (Nsmutabledictionary *) Getkeychainquery: (NSString *) Service {return[nsmutabledictionary Dictionarywithobjectsandkeys: (ID) Ksecclassgenericpassword, (ID) Ksecclass, service, (ID) Ksecattrservice, service, (ID) Ksecattraccount, (ID) Ksecattraccessibleafterfirstunlock, (ID) ksecattraccessible, nil];}+ (void) Save: (NSString *) Service data: (ID) Data {//Get Search DictionaryNsmutabledictionary *keychainquery =[self getkeychainquery:service]; //Delete old item before add New itemSecitemdelete ((cfdictionaryref) keychainquery); //Add New object to search dictionary (attention:the data format)[Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (ID) Ksecvaluedata]; //ADD item to keychain with the search dictionarySecitemadd ((cfdictionaryref) keychainquery, NULL);}+ (ID) Load: (NSString *) Service {IDRET =Nil; Nsmutabledictionary*keychainquery =[self getkeychainquery:service]; //Configure the search setting//Since in our simple case we is expecting only a single attribute to be returned (the password) we can set the Attribu Te Ksecreturndata to Kcfbooleantrue[Keychainquery SetObject: (ID) Kcfbooleantrue Forkey: (ID) Ksecreturndata]; [Keychainquery setobject: (ID) Ksecmatchlimitone Forkey: (ID) Ksecmatchlimit]; Cfdataref KeyData=NULL; if(Secitemcopymatching (Cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = =NOERR) { @try{ret= [Nskeyedunarchiver unarchiveobjectwithdata: (NSData *) KeyData]; } @catch(NSException *e) {NSLog (@"unarchive of%@ failed:%@", service, E); } @finally { } } if(KeyData) cfrelease (keyData); returnret;}+ (void) Delete: (NSString *) Service {nsmutabledictionary*keychainquery =[self getkeychainquery:service]; Secitemdelete ((cfdictionaryref) keychainquery);}@end
iOS get UUID