Get the iOS device UUID

Source: Internet
Author: User

Get to the UUID and save the UUID inside the keychain.

This allows you to read back from the keychain even after uninstalling the app.

However, the UUID will change after the brush or reload system.

The code uses the Cfuuid+keychain implementation method:

1. Create a Keychain management class

@implementationZzkeychainmanager+ (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) Deleteuuid: (NSString *) Service {nsmutabledictionary*keychainquery =[self getkeychainquery:service]; Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery);}@end

2. Creating the UUID Management class

#import "ZZUUIDManager.h" @implementation zzuuidmanagerstatic nsstring * Const Key_in_keychain = @ "Com.zzuuid.uuid"; + ( void) Saveuuid: (NSString *) uuid{if (uuid && uuid.length > 0) {[Zzkeychainmanager Save:key_in_keychai    N Data:uuid];    }}+ (NSString *) getuuid{nsstring *getudidinkeychain = (NSString *) [Zzkeychainmanager Load:key_in_keychain]; if (!getudidinkeychain | | [Getudidinkeychain isequaltostring:@ ""] | | [Getudidinkeychain Iskindofclass:[nsnull class]])        {Cfuuidref Puuid = cfuuidcreate (nil);        Cfstringref uuidstring = cfuuidcreatestring (nil, puuid);        NSString * result = (NSString *) cfbridgingrelease (cfstringcreatecopy (NULL, uuidstring));        Cfrelease (PUUID);        Cfrelease (uuidstring);               Getudidinkeychain = result;        [Zzkeychainmanager Save:key_in_keychain Data:result];    Getudidinkeychain = (NSString *) [Zzkeychainmanager Load:key_in_keychain]; } return getudidinkeychain;} + (void) deleteuuid{[ZzkeychaInmanager Deleteuuid:key_in_keychain];} 

3. Import #import "ZZUUIDManager.h"

Direct access

NSLog (@ "---->>uuid:%@", [Zzuuidmanager Getuuid]);

Reference: iOS get UUID

Get the iOS device UUID

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.