ios应用数据存储方式(XML属性列表-plist) - 转

时间:2023-03-09 13:35:04
ios应用数据存储方式(XML属性列表-plist) - 转

一.ios应用常用的数据存储方式 
  1.plist(XML属性列表归档) 
  2.偏好设置 
  3.NSKeydeArchiver归档(存储自定义对象) 
  4.SQLite3(数据库,关系型数据库,不能直接存储对象,要编写一些数据库的语句,将对象拆分存储) 
  5.Core Data(对象型的数据库,把内部环节屏蔽)

应用沙盒 
每个ios应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其它文件系统隔离。应用必须待在自己到沙盒里,其他应用不能访问该沙盒(提示:在ios8中已经开发访问)。 
应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Layer)

ios应用数据存储方式(XML属性列表-plist) - 转

模拟器应用用沙盒的根路径在: (apple是用用户名,模拟器版本9.4.1)

/Users/apple/Library/Developer/CoreSimulator/Devices/CCBBF90E-03CE-4B92-827F-345635A6CD7E/data/Containers/Data/Application/30EF0702-4062-40F9-B82C-974679878206

应用沙盒结构分析  应用程序包:(上图中的Layer)包含了所有的资源文件和可执行文件。 
1) Documents:保存应用运行时生成的需要持久性的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录。 
2) tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录 
3) Library 目录:这个目录下有两个子目录:Caches 和 Preferences
  Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大,不需要备份的非重要数据。 
  Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录。

4) AppNameApp 目录:这是应用程序的程序包目录,包含应用程序的本身。由于应用程序必须经过签名,所以您在运行时不能对这个目录中的内容进行修改,否则可能会使应用程序无法启动。

四.应用沙盒常见的获取方式 
  1.沙盒根目录:NSString *home = NSHomeDirectory();  Documents目录两种方法

NSString *homeDir = NSHomeDirectory();

  2.利用沙盒根目录拼接”Documents”字符串

NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@”Documents”];//不建议采用,因为新版本的操作系统可能会修改目录名

  3.利用 NSSearchPathForDirectoriesInDomains 函数

//NSUserDomainMask代表从用户文件下找
//YES代表展开路径中的波浪字符”~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);//在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素
NSString *documents = [array objectAtIndex:];

  4. 获取 tmp 目录 

NSString *tmp = NSTemporaryDirectory(); 

  5.Library/Caches:(跟Documents类似的2种方法)

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:];

  6.利用沙盒根目录拼接”Caches”字符串

  7.利用 NSSearchPathForDirectoriesInDomain 函数(将函数的2个参数改为:NSCachesDirectory即可)

  8.Library/Preference:通过 NSUserDefaults 类存取该目录下的设置信息

  9.获取应用程序程序包中资源文件路径的方法: 例如获取程序包中一个图片资源(apple.png)路径的方法:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

  代码中的mainBundle类方法用于返回一个代表应用程序包的对象。

代码:

#define CURRENT_SCREEN_WIDTH     [UIScreen mainScreen].bounds.size.width
#define CURRENT_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height - 64)
#define BUTTON_WIDTH 80
#define BUTTON_HEIGHT 40 @interface ViewController () //保存数据按钮
@property(nonatomic,strong) UIButton *saveButton;
//读取数据按钮
@property(nonatomic,strong) UIButton *readButton; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self initControl];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} //初始化控件
- (void)initControl{
_saveButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/ - BUTTON_WIDTH/,
CURRENT_SCREEN_HEIGHT/ - BUTTON_HEIGHT,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_saveButton setTitle:@"保存数据" forState:UIControlStateNormal];
[_saveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_saveButton addTarget:self
action:@selector(saveClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_saveButton]; _readButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/ - BUTTON_WIDTH/,
_saveButton.frame.origin.y + _saveButton.frame.size.height + ,
BUTTON_WIDTH,
BUTTON_HEIGHT)];
[_readButton setTitle:@"读取数据" forState:UIControlStateNormal];
[_readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[_readButton addTarget:self
action:@selector(readClick)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_readButton]; } - (void)saveClick{
//获取应用程序目录
NSString *home = NSHomeDirectory();
NSLog(@"应用程序目录:%@",home); //NSUserDomainMask在用户目录下查找
//YES 代表用户目录的~
//NSDocumentDirectory查找Documents文件夹
//建议使用如下方法动态获取
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSLog(@"Documents文件夹路径:%@",doc); //拼接文件路径
NSString *path = [doc stringByAppendingString:@"/abc.plist"]; //NSArray *array = @[@"ios",@"23"];
//[array writeToFile:path atomically:YES]; //NSDictionary *dict = @{@"name":@"ios",@"age":@"28"};
//[dict writeToFile:path atomically:YES]; /*
plist只能存储系统自带的一些常规的类,也就是有writeToFile方法的对象才可以使用plist保持数据
字符串/字典/数据/NSNumber/NSData ...
*/ //自定义的对象不能保存到plist中
DBPerson *person = [[DBPerson alloc] init];
person.name = @"ios";
} - (void)readClick{
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *path = [doc stringByAppendingString:@"/abc.plist"];
//读取数据
//NSArray *array = [NSArray arrayWithContentsOfFile:path];
//NSLog(@"%@",array); //NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
//NSLog(@"name = %@",[dict objectForKey:@"name"]);
//NSLog(@"age = %@",[dict objectForKey:@"age"]);
} @end

五.属性列表 
  1.属性列表是一种XML格式的文件,拓展名为plist。 
  2.如果对象是NSString,NSDictionary,NSArray,NSData,NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中