沙盒简介:沙盒文件系统目录,每个iOS应用都有自己引用沙盒,iOS系统不允许访问其他应用的沙盒,iOS只开放了一部分沙盒:docunment,library(包含caches和preference),temp;
沙盒路径
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//获取偏好设置文件夹
// NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, <#NSSearchPathDomainMask domainMask#>, <#BOOL expandTilde#>)
//获取临时文件夹
// NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, <#NSSearchPathDomainMask domainMask#>, <#BOOL expandTilde#>)
NSLog(@"%@",NSTemporaryDirectory());
}
- (void) testo2
{
//获取caches文件夹
NSString * caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",caches);
}
- (void)test01
{
//获取沙盒路径
// NSLog(@"%@",NSHomeDirectory());
//获取Bundle路径
// NSLog(@"%@",[NSBundle mainBundle].bundlePath);
// [NSBundle mainBundle].bundlePath;
//获取Documents文件夹
//1.获取沙盒路径
// NSString * homeDir = NSHomeDirectory();
//2.拼接字符串 (方式一)
// NSString * documentsPath = [homeDir stringByAppendingString:@"/Documents"];
//方式二:作为路径的一部分
// NSString * documentsPath = [homeDir stringByAppendingPathComponent:@"Documents"];
//方式三 参数1:目标文件夹 参数2:作用域 参数3:是否展开波浪线
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO) lastObject];
NSLog(@"%@",documentsPath);
}
- (void)viewDidLoad {
[super viewDidLoad];
//读取数据
//1.获取documents路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.拼接文件名
NSString * filePath = [path stringByAppendingPathComponent:@"names.plist"];
//3.读取
NSArray * namesArr = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",namesArr);
}
- (void) test01
{
// Do any additional setup after loading the view, typically from a nib.
//1.,创建数据
NSArray * names = [NSArray arrayWithObjects:@"Jack",@"Rose",@"Tom",@"AngleBaby",@"小明", nil];
//2.获取documents路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//3.拼接文件名
NSString * filePath = [path stringByAppendingPathComponent:@"names.plist"];
//4.存储 参数2:是否允许原子型写入
[names writeToFile:filePath atomically:YES];
NSLog(@"%@",filePath);
}
偏好存储preference
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//读取偏好设置
//创建NSUserDefaults对象
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
//获取数据
BOOL isAuto = [userDefaults boolForKey:@"isAutologin"];
NSString * name = [userDefaults objectForKey:@"name"];
NSString * pwd = [userDefaults objectForKey:@"pwd"];
NSLog(@"%d---%@----%@",isAuto,name,pwd);
}
- (void) test01
{
//偏好设置存储
//创建NSUserDefaults对象
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
//设置数据
[userDefaults setBool:YES forKey:@"isAutologin"];
[userDefaults setObject:@"JackMeng" forKey:@"name"];
[userDefaults setObject:@"123" forKey:@"pwd"];
//立即存储
[userDefaults synchronize];
NSLog(@"%@",NSHomeDirectory());
}
归档(保存数据)
反归档(读取数据)
import "CZPerson.h"
@implementation CZPerson
//归档 告诉系统要存储对象的哪些属性
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.phone forKey:@"phone"];
}
//反归档 读取对象的哪些属性
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init])
{
self.name = [aDecoder decodeObjectForKey:@"name"];
self.phone = [aDecoder decodeObjectForKey:@"phone"];
}
return self;
}
#import "ViewController.h"
#import "CZPerson.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//反归档
//1.获取documents路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2.拼接文件名
NSString * fileName = [path stringByAppendingPathComponent:@"contact.app"];
//3.反归档读取数据
CZPerson * person = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
NSLog(@"name === %@ phone == %@",person.name,person.phone);
}
- (void) test01
{
//创建联系人对象
CZPerson * person = [[CZPerson alloc] init];
//给Person属性赋值
person.name = @"JackMeng";
person.phone = @"10086";
//1.获取documents路径
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//2,拼接文件名
NSString * fileName = [path stringByAppendingPathComponent:@"contact"];
//通过归档的方式存储
[NSKeyedArchiver archiveRootObject:person toFile:fileName];
NSLog(@"%@",fileName);
}