At present, there are many ways to localize storage, and the following are commonly used:
1. Temporary cache
Let's start with the temporary cache, the temporary cache is typically used to manage some of the things that are commonly used globally in your application. such as the current user ID or the current location information.
The common way is to write a static variable and then use the class method call. (or a singleton class is OK)
Static Nsmutabledictionary *_cachedic;
#pragmaMark-Temporary cache (disappears after exiting the app) + (ID) Cachevalueforkey: (sccachekey) key{return[Self cachedic] valueforkey:[nsstring stringWithFormat:@"%d", Key]];}+(void) Setcachevalue: (ID) value Forkey: (sccachekey) key{[[Self cachedic] setobject:value forkey:[nsstring stringWithFormat:@"%d", Key]];}+(void) Deletecacheforkey: (Sccachekey) key{[[Self cachedic] removeobjectforkey:[nsstring stringWithFormat:@"%d", Key]];}
2. Local storage nsuserdefaults, by KV storage, will still exist after reboot. Disadvantage 1: can only store nsstring content, disadvantage 2: Delete the app will disappear
#pragma mark-field persistent cache (saved in database) + (NSString *) Storevalueforkey: (scstorekey) key{ return [[Nsuserdefaults Standarduserdefaults] valueforkey:[@ (key) stringvalue];} + (void) Setstorevalue: (NSString *) value Forkey: (scstorekey) key{ [[Nsuserdefaults Standarduserdefaults] Setobject:value forkey:[@ (key) stringvalue]; [[Nsuserdefaults standarduserdefaults] synchronize];} + (void) Deletestorevalueforkey: (scstorekey) key{ [[Nsuserdefaults Standarduserdefaults] removeobjectforkey:[@ (Key) stringvalue]]; [[Nsuserdefaults standarduserdefaults] synchronize];}
3.NSKeyedArchiver (archive) to store custom objects. Cons: Deleting apps will disappear.
#pragmaMark-Object Persistent cache (saved in local file) + (void) Setstoreobject: (nsobject<nscoding> *) obj Forkey: (scobjectkey) key{nsstring*path=[scsysconfig filepathbyname:[nsstring stringWithFormat:@"%d.domain", Key]]; [Nskeyedarchiver archiverootobject:obj Tofile:path];}+ (nsobject<nscoding> *) Storeobjectforkey: (scobjectkey) key{nsstring*path=[scsysconfig filepathbyname:[nsstring stringWithFormat:@"%d.domain", Key]]; NSObject<NSCoding> *obj=[Nskeyedunarchiver Unarchiveobjectwithfile:path]; returnobj;}+(void) Deletestoreobjectforkey: (scobjectkey) key{nsstring*path=[scsysconfig filepathbyname:[nsstring stringWithFormat:@"%d.domain", Key]]; [Scfileoper Removefile:path];}
Nskeyedarchiver the use of the method can refer to this blog: http://www.cnblogs.com/xiaobaizhu/p/4011332.html
4.KeyChain of use, after removing the app, reinstall still exists. (as long as Boundleid consistent)
A keychain is a keychain, which is a way to save data after the application has been deleted. It is related to the Boundleid of storage and application. For example, Baidu Bar is implemented in the application after the deletion, and then reinstall, if the token is still valid, no more landing.
Keychain use the official interface slightly annoying, generally use a third-party library, specifically, you can refer to my blog: http://www.cnblogs.com/rayshen/p/4671477.html
Demo:https://github.com/rayshen/localdatademo
An approach to IOS on local persistent storage