但是这对真机不管用,因为在真机环境下,App在Xcode中的Resources文件夹都是不可写的.所以我们要在App第一次运行时将需要修改且长久保存的数据放到Document目录下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <code class="hljs objectivec">//在Document目录中初始化地图数据文件,因为该目录下文件可写且可以持久保存 +(void)initDataFile{ NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //取得目标文件路径 sMapInfoInDocPath = [docPath stringByAppendingPathComponent:@"MapInfo.plist"]; NSFileManager *fm = [NSFileManager defaultManager]; //如果目标文件不存在说明是App第一次运行,需要将相关可修改数据文件拷贝至目标路径. if (![fm fileExistsAtPath:sMapInfoInDocPath]) { NSError *error = nil; //取得源文件路径 NSString *srcPath = [[NSBundle mainBundle] pathForResource:MAP_INFO_FILE ofType:@"plist"]; if (![fm copyItemAtPath:srcPath toPath:sMapInfoInDocPath error:&error]) { CCLOG(@"ERR:copy file failed:%@",error); } } }</code> |
注意当删除一个App后,该App先前存放在Document目录下的所有文件都会被删除.