1 // 2 // ViewController.m 3 // IOS_0113_本地存储 4 // 5 // Created by ma c on 16/1/13. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "BWLoginDataModel.h" 11 #import "BWSecondViewController.h" 12 13 @interface ViewController () 14 15 @end 16 17 @implementation ViewController 18 /* 19 数据本地化(数据持久化)- 本地存储 20 1.NSUserDefault属性列表存储 - 轻量级数据,存储类型有限 21 2.归档 - 大量数据,存储类型能够扩展到自定义对象 22 3.本地数据库 23 24 无论是属性列表仍是归档,都是将数据保存到后台,前端任意一个界面都能访问 25 */ 26 27 - (void)viewDidLoad { 28 [super viewDidLoad]; 29 /* 30 归档 - 本地化数据模型 31 <NSCoding> - 要对哪一个类的对象进行归档,就让哪一个类实现这个协议 32 */ 33 self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; 34 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeView)]; 35 [self.view addGestureRecognizer:tap]; 36 37 BWLoginDataModel *loginModel = [[BWLoginDataModel alloc] init]; 38 loginModel.name = @"123456"; 39 loginModel.password = @"654321"; 40 41 //沙盒路径 - 每个应用都有本身的沙盒,IOS机制 42 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"account.data"]; 43 //NSLog(@"%@",filePath); 44 45 //将一个归档对象归档到目录下 46 if ([NSKeyedArchiver archiveRootObject:loginModel toFile:filePath]) { 47 NSLog(@"归档成功"); 48 } 49 else 50 NSLog(@"归档失败"); 51 52 } 53 54 - (void)changeView 55 { 56 BWSecondViewController *secondVC = [[BWSecondViewController alloc] init]; 57 58 [self presentViewController:secondVC animated:YES completion:nil]; 59 } 60 61 #pragma mark - 属性列表基础 62 - (void)useUserDefault 63 { 64 /* 65 //属性列表NSUserDefault(全局惟一,不用建立) 66 67 某些数据当存储本地以后,下次再次调用的时候,不用再次从新赋值,而是从本地直接获取 68 若是没有必要,别向属性列表存储大量数据 69 通常用来存储简单的全局都能使用到的数据 70 e.g. DeviceToken 设备令牌 71 SessionID 安全口令 72 ps:属性列表的存储和读取速度都是及时的,而归档和数据库都是有存储延时的 73 pss:自动登陆 74 把用户的帐号,密码存储到属性列表中 75 76 */ 77 // NSString *str = @"bowen"; 78 // [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"sister"]; 79 // NSString *newStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"sister"]; 80 // NSLog(@"%@",newStr); 81 // NSLog(@"%@",[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); 82 83 //注意别删除错了 84 //[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sister"]; 85 //NSLog(@"%@",newStr); 86 87 } 88 89 - (void)didReceiveMemoryWarning { 90 [super didReceiveMemoryWarning]; 91 // Dispose of any resources that can be recreated. 92 } 93 94 @end
1 // 2 // BWSecondViewController.m 3 // IOS_0113_本地存储 4 // 5 // Created by ma c on 16/1/13. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "BWSecondViewController.h" 10 #import "BWLoginDataModel.h" 11 12 @interface BWSecondViewController () 13 14 @end 15 16 @implementation BWSecondViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 self.view.backgroundColor = [UIColor cyanColor]; 21 22 //解档 23 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"account.data"]; 24 BWLoginDataModel *dataModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 25 NSLog(@"%@",dataModel); 26 } 27 28 - (void)didReceiveMemoryWarning { 29 [super didReceiveMemoryWarning]; 30 // Dispose of any resources that can be recreated. 31 } 32 33 @end
1 // 2 // BWLoginDataModel.h 3 // IOS_0113_本地存储 4 // 5 // Created by ma c on 16/1/13. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface BWLoginDataModel : NSObject<NSCoding> 12 13 14 @property (nonatomic, copy) NSString *name; 15 @property (nonatomic, copy) NSString *password; 16 17 18 @end 19 20 21 // 22 // BWLoginDataModel.m 23 // IOS_0113_本地存储 24 // 25 // Created by ma c on 16/1/13. 26 // Copyright (c) 2016年 博文科技. All rights reserved. 27 // 28 29 #import "BWLoginDataModel.h" 30 31 @implementation BWLoginDataModel 32 33 - (NSString *)description 34 { 35 return [NSString stringWithFormat:@"name:%@ password:%@", self.name,self.password]; 36 } 37 38 #pragma mark - 归档方法 39 - (void)encodeWithCoder:(NSCoder *)aCoder 40 { 41 //按照规定的Key对属性进行编码操做 42 [aCoder encodeObject:self.name forKey:@"name"]; 43 [aCoder encodeObject:self.password forKey:@"password"]; 44 45 } 46 #pragma mark - 解档方法 47 - (id)initWithCoder:(NSCoder *)aDecoder 48 { 49 self = [super init]; 50 if (self) { 51 //归档和解档的key能够随意写,但他们属性对应的key必须相同 52 _name = [aDecoder decodeObjectForKey:@"name"]; 53 _password = [aDecoder decodeObjectForKey:@"password"]; 54 } 55 return self; 56 } 57 58 @end