沙盒機制就是指ios應(yīng)用程序只能在為該程序創(chuàng)建的文件系統(tǒng)中讀取文件方咆,不可以去其它地方訪問,所有的非代碼文件都要保存在此蟀架,圖像瓣赂,圖標,聲音片拍,映像煌集,屬性列表,文本文件等捌省。(ios8之前是如此,iOS8新開放了一種對幾個固定系統(tǒng)區(qū)域的擴展機制extension,可以在一定程度上彌補iOS的沙盒機制對應(yīng)用間通信的限制).每個應(yīng)用程序都有自己的存儲空間 ;不能翻過自己的圍墻去訪問別的存儲空間;應(yīng)用程序請求的數(shù)據(jù)都要通過權(quán)限檢測苫纤,假如不符合條件的話,不會被放行
- plist儲存
儲存位置 Documents -> data.plist
存數(shù)據(jù):
- (IBAction)save:(id)sender {
/**
*
獲取沙盒的根路徑
NSString *home = NSHomeDirectory();
拼接Documents路徑
NSString *docPath = [home stringByAppendingString:@"/Documents"];
NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
NSDocumentDirectory : 查找Documents文件夾
NSUserDomainMask : 在用戶的應(yīng)用程序下查找
YES 把路徑展開 NO 當前應(yīng)用的根路徑 == ~
NO ~/Documents
*/
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",docPath);
// 拼接文件路徑
NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
// 只有具備writeToFile:的對象才能使用plist存儲纲缓,NSArray
NSArray *array = @[@1,@2,@"123"];
[array writeToFile:filePath atomically:YES];
}
讀數(shù)據(jù):
- (IBAction)read:(id)sender {
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",docPath);
// 拼接文件路徑
NSString *filePath = [docPath stringByAppendingPathComponent:@"data.plist"];
NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",data);
}
2.NSKeyedArchiver
存儲位置 Library ->Caches ->data
(1)歸檔卷拘,解檔一個對象
- (IBAction)save:(id)sender {
HMPerson *person = [HMPerson new];
person.age = 18;
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件路徑
NSString *filePath = [docPath stringByAppendingPathComponent:@"person.data"];
[NSKeyedArchiver archiveRootObject:person toFile:filePath];
}
- (IBAction)read:(id)sender {
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
// 拼接文件路徑
NSString *filePath = [docPath stringByAppendingPathComponent:@"person.data"];
HMPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d",p.age);
}
#import <Foundation/Foundation.h>
@interface HMPerson : NSObject<NSCoding>
@property (nonatomic, assign) int age;
@end
#import "HMPerson.h"
@implementation HMPerson
// 在對象歸檔的時候調(diào)用
// 哪些屬性需要歸檔
// 這些屬性怎么歸檔
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:_age forKey:@"age"];
}
// 在對象解檔的時候調(diào)用
// 哪些屬性需要解檔
// 這些屬性怎么解檔
- (id)initWithCoder:(NSCoder *)aDecoder
{
// 當父類實現(xiàn)了NSCoding,就能調(diào)用 [super initWithCoder]
if (self = [super init]) {
_age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
- NSUserDefault
儲存位置 Library ->Preferences
- (IBAction)save:(id)sender {
// [NSUserDefaults standardUserDefaults]可以直接操作偏好設(shè)置文件夾
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 自動幫我們生成一個plist文件存放在偏好設(shè)置的文件夾
[defaults setObject:@"hm" forKey:@"account"];
// 同步:把內(nèi)存中的數(shù)據(jù)和沙盒同步
[defaults synchronize];
}
- (IBAction)read:(id)sender {
// [NSUserDefaults standardUserDefaults]可以直接操作偏好設(shè)置文件夾
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%@",[defaults objectForKey:@"account"]);
// NSDictionary *dict = @{@"account":@"hm"};
//
// dict writeToFile:<#(NSString *)#> atomically:<#(BOOL)#>
}