Plist文件的读取
NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
Plist文件的写入
[myDictionary writeToFile:path atomically:YES];
这样做在模拟器上运行是没有任何问题的,但是在真机当中,你就会发现只能读取不能写入,原因是在真机上,自己包里的文件,不管你是否plist还是.jpg等,都只能读,如果要写就要拷贝到安装后的doc目录下.
解决方案是:
修改文件的路径即可
NSArray *storeFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doucumentsDirectiory = [storeFilePath objectAtIndex:0];
NSString *path=[doucumentsDirectiory stringByAppendingPathComponent:@"myPlist.plist"];
修改前:
/var/mobile/Applications/A528A4DE-F391-4247-8C5D-9386DC415A00/Box.app/myPlist.plist
修改后:
/var/mobile/Applications/A528A4DE-F391-4247-8C5D-9386DC415A00/Documents/myPlist.plist