我有一个应用程序,我在退出(或转到后台)之前将数据保存到文件中,并在重新启动应用程序时恢复从文件读取的数据。问题是这在模拟器上完美运行,但不适用于真实设备。 这是我用来获取路径的代码(在applicationDidFinishLaunching上):
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:@"archive"];
NSLog(@"Reading from filePath:%@",path);
if ([[NSFileManager defaultManager]fileExistsAtPath:path])
{
//Init data from file
}
else
{
//Present the view where the users has to enter info for the first time (this is what always gets executed)
}
这是我用来保存数据的代码(在applicationDidEnterBackground上):
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingString:@"archive"];
NSLog(@"Reading from filePath:%@",path);
NSMutableData *data=[[NSMutableData alloc]init];
//Put info to save in data
[data writeToFile:path atomically:YES];
我已经检查了iphone的控制台,并且在加载信息时打印的是一个看起来正常的目录(我这里没有设备所以我无法发布确切的目录),然后我看到一个拒绝file-write-create错误如下:
unknown sandboxd[1332] <Notice>: AppName(1328) deny file-write-create
当我再次访问设备(明天)时,我将更新帖子并打印确切的目录。知道为什么会这样吗?
答案 0 :(得分:1)
尝试使用stringByAppendingPathComponent
代替stringByAppendingString
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"archive"];