0

我有一个应用程序,我在退出(或进入后台)之前将数据保存到文件中,并在重新启动应用程序时恢复从文件中读取的数据。问题是这在模拟器上完美运行,但在真实设备上却不行。这是我用来获取路径的代码(在 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 的控制台,在加载信息时它打印的是一个看起来正常的目录(我这里没有设备,所以我不能发布确切的目录),然后我看到一个拒绝文件写入- 创建这样的错误:

unknown sandboxd[1332] <Notice>: AppName(1328) deny file-write-create

当我再次访问该设备时(明天),我将使用打印的确切目录更新帖子。知道为什么会这样吗?

4

1 回答 1

1

尝试使用stringByAppendingPathComponent而不是stringByAppendingString

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"archive"];
于 2012-04-08T01:11:07.980 回答