iphone write to file fails

你说的曾经没有我的故事 提交于 2019-12-18 09:39:39

问题


I'm writing some data to a plist file but it fails on the device but not in the simulator. I'm lost, don't know where to look for a solution.

This is my code:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:nameField.text forKey:@"name"];
[dict setObject:emailField.text forKey:@"email"];
BOOL ret = [dict writeToFile:[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"]] atomically:YES];
NSLog(@"%d",ret);
[dict release];

The log returns 0 on the device, but 1 in the simulator.

Can somebody help me out?


回答1:


Why are you trying to write in resource directory? Have you checked this?




回答2:


This fails on the device because you cannot write to the application's bundle folder, only to the /tmp, Documents, and Cache folders. If users were able to modify the bundle, the digital signature for the app would no longer be valid and no one would be able to reliably obtain your intact app from the App Store and run it.

This works on the simulator, however, because your operating system will let you write to pretty much anywhere in your home folder, including the application bundle folder. OS X functionality is not restricted in the same way iOS's is restricted.

This is part of iOS's sandbox approach to security. The solution is to write to the /tmp, Documents or Cache folder, instead.



来源:https://stackoverflow.com/questions/3386973/iphone-write-to-file-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!