在這里本人將以往做過的項(xiàng)目一一做了總結(jié)潘悼,并且拆分成一個(gè)個(gè)細(xì)小的功能模塊(這里不包括UI界面搭建)律秃,封裝抽類成一個(gè)個(gè)獨(dú)立的功能。會(huì)把涉及到的功能點(diǎn)和知識點(diǎn)做詳細(xì)的注釋治唤,便于理解友绝、學(xué)習(xí)、使用肝劲。
重要數(shù)據(jù)本地存儲
重要數(shù)據(jù)這里所要說的是用戶數(shù)據(jù),包含用戶相關(guān)信息郭宝,以便用戶操作APP辞槐,前端和服務(wù)器的數(shù)據(jù)交互。 數(shù)據(jù)本地持久化(所謂的持久化粘室,就是將數(shù)據(jù)保存到硬盤中榄檬,使得在應(yīng)用程序或機(jī)器重啟后可以繼續(xù)訪問之前保存的數(shù)據(jù))方式:
- 寫入本地文件(plist文件)</br>
- NSUserDefaults(Preference偏好設(shè)置)</br>
- NSKeyedArchiver(歸檔) </br>
- coreData </br>* sqlite3 </br>
- FMDB(FMDB是S對qlite數(shù)據(jù)庫的封裝)</br>
這里主要講解用戶信息數(shù)據(jù)寫本地文件(plist文件)
1.創(chuàng)建單例,在單例里面將數(shù)據(jù)存入沙盒文件中
#import <Foundation/Foundation.h>
@class UserModel;
@interface UserManager : NSObject
// 用戶信息
@property (nonatomic, strong) UserModel * activeUser;
/** * 單例
* @return 實(shí)例對象
*/
+ (instancetype)shareManager ;
/**
* 沙盒保存用戶信息
* @param responseObject 服務(wù)器返回的用戶數(shù)據(jù)
* @param errorMessage 錯(cuò)誤信息
* @return 是否保存成功 */
- (BOOL)saveWithResponseObjectAsUserInfo:(NSDictionary *)responseObject withErrorMessage:(NSString *)errorMessage ;
@end
#import "UserManager.h"
#import "UserModel.h"
@implementation UserManager
/**
* 單例
* * @return 實(shí)例對象
*/
static UserManager * defualt_shareMananger = nil;
+ (instancetype)shareManager {
static dispatch_once_t onceToken;
_dispatch_once(&onceToken, ^{
if (defualt_shareMananger == nil) {
defualt_shareMananger = [UserManager new];
} });
return defualt_shareMananger;
}
#pragma mark - 沙盒存儲-寫本地
/**
* 沙盒保存用戶信息 *
* @param responseObject 服務(wù)器返回的用戶數(shù)據(jù)
* @param errorMessage 錯(cuò)誤信息 *
* @return 是否保存成功 */
- (BOOL)saveWithResponseObjectAsUserInfo:(NSDictionary *)responseObject withErrorMessage:(NSString *)errorMessage {
if (!isNULL(responseObject)) {
// 獲取到存儲路徑
NSString *filePath = [self documentFilePathWithUserInfo];
// 用戶信息轉(zhuǎn)化 UserModel * userInfo = [UserModel mj_objectWithKeyValues:responseObject];
// 判斷用戶是否正常登陸 if (userInfo.auth_token.length == 0 || userInfo.userID.length == 0) { if (errorMessage != NULL) { errorMessage = @"登錄失敗"; return NO;
} }
// 將數(shù)據(jù)寫入沙盒
BOOL result = [responseObject writeToFile:filePath atomically:YES];
if (result) { if (errorMessage != NULL) errorMessage = @"登錄成功";
return YES;
}else {
if (errorMessage != NULL) errorMessage = @"登錄失敗"; return NO;
} }else {
if (errorMessage != NULL) errorMessage = @"登錄失敗"; return NO;
}}
/** * 沙盒路徑 *
* @return
*/
- (NSString *)documentFilePathWithUserInfo {
// 一、獲取到當(dāng)前的Document文件夾的路徑
// 1衔统、NSSearchPathDirectory directory 第一個(gè)參數(shù):我們要顯示的文件夾是哪一個(gè)
// 2鹿榜、NSSearchPathDomainMask domainMask 第二個(gè)參數(shù):是在哪個(gè)域(用戶權(quán)限)下面
// 3海雪、BOOL expandTilde 第三個(gè)參數(shù) :預(yù)留參數(shù)
NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 將前面的路徑格式和后面的普通的字符串格式鏈接在一起,并且以路徑格式返回舱殿。
NSString * filePath = [documentPath stringByAppendingPathComponent:@"userInfo"]; return filePath;}
/**
* 當(dāng)前用戶 *
* @return <#return value description#>
*/
- (UserModel *)activeUser {
if (_activeUser != nil) { return _activeUser;
}
// 獲取數(shù)據(jù)存儲路徑
NSString *filePath = [self documentFilePathWithUserInfo];
// 查詢數(shù)據(jù)是否存在
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (isExist) {
// 取出數(shù)據(jù)
NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
if (dic) {
UserModel * userModel = [UserModel mj_objectWithKeyValues:dic];
if (userModel) {
_activeUser = userModel;
} }
return _activeUser;
}else {
return nil;
}}
@end
2.在APP登錄后奥裸,服務(wù)器會(huì)將對應(yīng)的用戶數(shù)據(jù)返回給你,這時(shí)需要調(diào)用沙盒存儲方法沪袭,把用戶數(shù)據(jù)存儲到本地湾宙,以便用戶的其他操作。
其他的持久化方式也會(huì)一一介紹其用法和用途冈绊,可以繼續(xù)關(guān)注這個(gè)博客專欄侠鳄。