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]