简体   繁体   English

iPhone写入文件失败

[英]iphone write to file fails

I'm writing some data to a plist file but it fails on the device but not in the simulator. 我正在将一些数据写入plist文件,但它在设备上失败,但在模拟器中失败。 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. 该日志在设备上返回0,但在模拟器中返回1。

Can somebody help me out? 有人可以帮我吗?

This fails on the device because you cannot write to the application's bundle folder, only to the /tmp, Documents, and Cache folders. 这在设备上失败,因为您无法写入应用程序的bundle文件夹,而只能写入/ tmp,Documents和Cache文件夹。 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. 如果用户能够修改捆绑软件,则该应用程序的数字签名将不再有效,并且没有人能够从App Store可靠地获取完整的应用程序并运行它。

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. OS X功能的限制与iOS的限制不同。

This is part of iOS's sandbox approach to security. 这是iOS安全性沙盒方法的一部分。 The solution is to write to the /tmp, Documents or Cache folder, instead . 解决方案是改为写入/ tmp,Documents或Cache文件夹

Why are you trying to write in resource directory? 为什么要尝试在资源目录中写? Have you checked this ? 你检查吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM