[IOS keychain Local long-term key-value store]

Source: Internet
Author: User
Tags value store

At present, the local storage methods are roughly: Sqlite,coredata,nsuserdefaults. But they are deleted after the app is deleted, and if you use storage for a long time, you can use the Keychain keychain.

CHKeychain.h

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

Chkeychain.m

#import "CHKeychain.h"@implementationChkeychain+ (Nsmutabledictionary *) Getkeychainquery: (NSString *) Service {return[nsmutabledictionary Dictionarywithobjectsandkeys: (__bridgeID) Ksecclassgenericpassword, (__bridgeID) Ksecclass, service, (__bridgeID) Ksecattrservice, service, (__bridgeID) Ksecattraccount, (__bridgeID) Ksecattraccessibleafterfirstunlock, (__bridgeID) ksecattraccessible, nil];}+ (void) Save: (NSString *) Service data: (ID) Data {//Get Search DictionaryNsmutabledictionary *keychainquery =[self getkeychainquery:service]; //Delete old item before add New itemSecitemdelete ((__bridge cfdictionaryref) keychainquery); //Add New object to search dictionary (attention:the data format)[Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (__bridgeID) Ksecvaluedata]; //ADD item to keychain with the search dictionarySecitemadd ((__bridge 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: (__bridgeID) Ksecreturndata]; [Keychainquery setobject: (__bridgeID) Ksecmatchlimitone Forkey: (__bridgeID) Ksecmatchlimit]; Cfdataref KeyData=NULL; if(Secitemcopymatching (__bridge 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) Delete: (NSString *) Service {nsmutabledictionary*keychainquery =[self getkeychainquery:service]; Secitemdelete ((__bridge cfdictionaryref) keychainquery);}@end

Call: (Take the generation UUID as an example)

#defineUuidkey @ "UUID"#pragmamark--Get device uuid-(nsstring*) uuid{if([Chkeychain Load:uuidkey]) {nsstring*result =[Chkeychain Load:uuidkey]; _phoneuuid=result; NSLog (@"Mobile uuid:%@ already exists", result); returnresult; }    Else{cfuuidref Puuid=cfuuidcreate (nil); Cfstringref uuidstring=cfuuidcreatestring (nil, puuid); NSString* result = (NSString *) Cfbridgingrelease (Cfstringcreatecopy (NULL, uuidstring));        Cfrelease (PUUID);        Cfrelease (uuidstring);        [Chkeychain Save:uuidkey Data:result]; NSLog (@"first time to create a phone uuid:%@", result); _phoneuuid=result; returnresult; }    returnNil;}

In the past seems certain to be through their own new plist file configuration developer account to achieve, now seems to have an interface, relatively simple.

[IOS keychain Local long-term key-value store]

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.