#pragma mark - 字段持久緩存(保存在數據庫) +(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]; }
2.本地存儲NSUserdefaults,按KV存儲,重啟後還會存在。缺點1:只能存儲NSString內容,缺點2:刪除APP後會消失
#pragma mark - 字段持久緩存(保存在數據庫) +(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(歸檔),可存儲自定義對象。缺點:刪除APP後會消失。
#pragma mark - 對象持久緩存(保存在本地文件) +(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]; return obj; } +(void)deleteStoreObjectForKey:(SCObjectKey)key{ NSString *path=[SCSysconfig filePathByName:[NSString stringWithFormat:@"%d.domain",key]]; [SCFileOper removeFile:path]; }
4.KeyChain的使用,刪除APP後,重新安裝還存在。(只要BoundleID一致) Keychain即鑰匙串,是用以在應用刪除後還可以保存數據的一種方法。它的存儲和應用的BoundleID有關。比如百度貼吧就實現了在應用刪除後,再重新安裝,如果Token還是有效的話,無需再登陸。