(Absolutely useful) iOS gets UUID and uses keychain storage

Source: Internet
Author: User

Original link: http://blog.sina.com.cn/s/blog_5971cdd00102vqgy.html
(Absolutely useful) iOS gets UUID and uses keychain to store this blog post with pictures ( -- to- - -: +: -Reprint Tags: iOS it sen female UUID Keychain Category: iOS technology Udid is deprecated, using UUID as the device's unique identity. Once the UUID is acquired, if it is stored with Nsuserdefaults, when the program is unloaded and then re-loaded, the UUID obtained is different from the previous. Using the keychain storage ensures that the UUID does not change when the program unloads load. However, the UUID will still change when the machine is being brushed or the system is upgraded. But this is still the best solution so far, if there is a better solution, please leave a message. (The source of the solution I've sorted out: http://blog.k-res.net/archives/1081.html)first of all to write before the article oneself did not use the real machine test, and this period of time everyone add my QQ or private messages I am good, some even found my Wang Wang on, but I did not give you a timely reply is sorry. Go to the point, I then tried to write their own method, found that with the simulator can, but the real machine is not. The reason for this is that the previously generated UUID was not successfully stored by the keychain after the real machine was unloaded. So the problem is keychain. The Keychainitemwrapper Class I gave you before is Apple's official, but some friends reflect that the runtime crashes. In addition, the Code Signing entitlements is not rigorous enough to create a method. So, please come back with me again. 1Create a new project and look at your bundle ID. This bundle ID matches the bundle ID on the certificate you used when you tested the real machine. (Absolutely useful) iOS gets UUID and uses keychain storage such as mine is House.xianrou.xianrou2. Target-capabilities-keychain Sharing-On (absolutely useful) iOS gets UUID and uses keychain storage (absolutely useful) iOS to get UUID, and using keychain storage This step is primarily to open keychain sharing and change it from gray state to blue state on. The changes after opening are as follows: (absolutely useful) iOS gets the UUID and uses keychain storage (absolutely useful) iOS to get the UUID, and using keychain storage to the left of the directory will automatically generate entitlements files, do not need to create their own. That is, Bundle Identifier, Keychain sharing Keychain Groups, entitlements file Keychain The first element of Access Groups, they want to maintain the consistency shown. Set up later can run the next program, no problem can proceed to the next step. 3. The legendary UUID class and the Keychain class. Since Apple's Keychain method crashes and is somewhat complex, we can only save one UUID with the following simple method: (This is also my own Baidu's keychain copy others, and then change) UUID.h #importAngle brackets (foundation/Foundation.h)@interfaceUuid:nsobject+ (NSString *) Getuuid; @endUUID.M#import "UUID.h"#import "KeyChainStore.h"@implementationUUID+ (NSString *) getuuid{NSString* Struuid = (NSString *) [Keychainstore Load:@"Com.company.app.usernamepassword"]; //when the method is first executed, the UUID is empty if([Struuid isequaltostring:@""] || !struuid) { //method of generating a UUIDCfuuidref Uuidref =cfuuidcreate (Kcfallocatordefault); Struuid= (NSString *) Cfbridgingrelease (cfuuidcreatestring (kcfallocatordefault,uuidref)); //Save this UUID to keychain[Keychainstore Save:key_username_password data:struuid]; } returnStruuid;} @endKeyChainStore.h#importAngle brackets (foundation/Foundation.h)@interfaceKeychainstore:nsobject+ (void) Save: (NSString *) Service data: (ID) data;+ (ID) Load: (NSString *) service;+ (void) Deletekeydata: (NSString *) Service; @endKEYCHAINSTORE.M#import "KeyChainStore.h"@implementationKeychainstore+ (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: (__bridge NSData *) KeyData]; } @catch(NSException *e) {NSLog (@"unarchive of%@ failed:%@", service, E); } @finally { } } if(KeyData) cfrelease (keyData); returnret;}+ (void) Deletekeydata: (NSString *) Service {nsmutabledictionary*keychainquery =[self getkeychainquery:service]; Secitemdelete ((cfdictionaryref) keychainquery);} @endadd these two classes to a project4. Create a new PCH file, then the contents of the PCH file are as follows: #ifndef prefixheader_pch#definePrefixheader_pch#defineKey_username_password @ "Com.company.app.usernamepassword"#defineKey_username @ "Com.company.app.username"#defineKey_password @ "Com.company.app.password"#endifPCH File creation method can be referenced: http://blog.csdn.net/huang2009303513/article/details/40375235You might get an error in filling in the path to the prefix Header, the PCH file, and recently learned a better way $ (srcroot)/$ (PROJECT_NAME)/prefixheader.pch, where $ (project_name) is relative to the project name, is more convenient than the above method.5execute the following code inside VIEWCONTROLLER.M nsstring* uuid=[UUID Getuuid]; NSLog (@"uuid=%@", UUID); The resulting UUID is similar to this UUID=19aab430-9cb8-4325-acc5-d7d386b68960 then uninstall, and then rerun, see before and after the UUID is not the same!

(Absolutely useful) iOS gets UUID and uses keychain storage

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.