在iOS開發(fā)過程中注冊登錄往往是必不可少的, 那么如何保存用戶的這些注冊信息在本地呢?
- 示例
這里我創(chuàng)建了一個名為LoginController
的控制器, 有五個NSString
屬性, 分別是str0...str4
, 和一個TextField
的數組; 同時還有四個按鈕, 作用分別是保存信息到plist文件
, 加載pilst文件保存的信息
, 歸檔信息
, 加載歸檔文件里的信息
.
LoginController.h
代碼:
@interface LoginController : UIViewController<NSCoding>
@property (nonatomic) NSArray<UITextField *> *tfArray;
@property (nonatomic, copy) NSString *str0;
@property (nonatomic, copy) NSString *str1;
@property (nonatomic, copy) NSString *str2;
@property (nonatomic, copy) NSString *str3;
@property (nonatomic, copy) NSString *str4;
@end
注意必須要遵守
NSCoding
, 否則無法實現歸檔和解檔
保存方式一: plist文件保存
LoginController.m
代碼:
- 保存按鈕點擊事件
//保存數據
- (void)saveBtnClick {
NSMutableArray *dataArr = [NSMutableArray new];
for (int i = 0; i < self.tfArray.count; i++) {
[dataArr addObject:self.tfArray[i].text];
}
NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/person.plist"];
[dataArr writeToFile:str atomically:YES];
NSLog(@"用戶信息保存路徑: %@", str);
}
- 加載plist文件保存的信息的按鈕點擊事件
- (void)loadBtnClick {
NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/person.plist"];
NSArray *arr = [NSArray arrayWithContentsOfFile:str];
for (int i = 0; i < self.tfArray.count; i++) {
self.tfArray[i].placeholder = arr[i];
}
}
保存方式二: 歸檔
- 歸檔按鈕點擊事件
- (void)archiveBtnClick {
LoginController *per = [[LoginController alloc] init];
per.str0 = self.tfArray[0].text;
per.str1 = self.tfArray[1].text;
per.str2 = self.tfArray[2].text;
per.str3 = self.tfArray[3].text;
per.str4 = self.tfArray[4].text;
NSLog(@"per------%@%@%@", per.str0, per.str1, per.str3);
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//歸檔數據
[archive encodeObject:per forKey:@"me"];
//結束歸檔
[archive finishEncoding];
//將歸檔數據寫入磁盤
NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/guidang.zhengmengxin"];
[data writeToFile:str atomically:YES];
NSLog(@"歸檔:%@", str);
}
- 解檔按鈕加載解檔內容事件
- (void)loadArchiveBtnClick {
NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/guidang.zhengmengxin"];
NSData *data = [NSData dataWithContentsOfFile:str];
NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//解檔數據
LoginController *vc = [unarchive decodeObjectForKey:@"me"];
[unarchive finishDecoding];
NSLog(@"vc------%@%@%@", vc.str0, vc.str1, vc.str3);
self.tfArray[0].text = vc.str0;
self.tfArray[1].text = vc.str1;
self.tfArray[2].text = vc.str2;
self.tfArray[3].text = vc.str3;
self.tfArray[4].text = vc.str4;
}
必須實現的NSCoding
協議方法
我們先來看看NSCoding
協議的API
@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER
@end
LoginController.m
中實現協議的兩個方法:
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
self.str0 = [aDecoder decodeObjectForKey:@"str0"];
self.str1 = [aDecoder decodeObjectForKey:@"str1"];
self.str2 = [aDecoder decodeObjectForKey:@"str2"];
self.str3 = [aDecoder decodeObjectForKey:@"str3"];
self.str4 = [aDecoder decodeObjectForKey:@"str4"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.str0 forKey:@"str0"];
[aCoder encodeObject:self.str1 forKey:@"str1"];
[aCoder encodeObject:self.str2 forKey:@"str2"];
[aCoder encodeObject:self.str3 forKey:@"str3"];
[aCoder encodeObject:self.str4 forKey:@"str4"];
}