读写plist文件的奇怪问题
我有一个从 plist 文件中读取信息的应用程序。为此,我使用下面的代码: 为了
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
localizedPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
NSString *tel=[NSString stringWithFormat:@"tel:%@",[plist objectForKey:@"number"]];
NSURL *telephoneURL = [NSURL URLWithString:tel];
[[UIApplication sharedApplication] openURL:telephoneURL];
编写它,我使用以下代码:
- (IBAction) saveSetting:(id)sender{
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
NSLog([plist objectForKey:@"message"]);
[plist setValue:textMex.text forKey:@"message"];
NSLog([plist objectForKey:@"message"]);
NSLog([plist objectForKey:@"number"]);
[plist setValue:textNumero.text forKey:@"number"];
NSLog([plist objectForKey:@"number"]);
[plist setValue:@"NO" forKey:@"firstTime"];
[plist writeToFile:localizedPath atomically:YES];
[self aggiorna];
[settingScreen removeFromSuperview];
}
现在我有一个大问题,该应用程序在我的所有开发人员设备和模拟器中正常工作,并且该应用程序可以正确读取和写入文件。
我在 Apple 商店提交了该应用程序,但其他用户无法读取/写入该文件。
这是为什么呢?
I have an application that read info from I plist file. To do it I use this code below:
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
localizedPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
NSString *tel=[NSString stringWithFormat:@"tel:%@",[plist objectForKey:@"number"]];
NSURL *telephoneURL = [NSURL URLWithString:tel];
[[UIApplication sharedApplication] openURL:telephoneURL];
And to write it I use this code:
- (IBAction) saveSetting:(id)sender{
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
plistData = [NSData dataWithContentsOfFile:localizedPath];
plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&error];
if (!plist) {
NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]);
[error release];
}
NSLog([plist objectForKey:@"message"]);
[plist setValue:textMex.text forKey:@"message"];
NSLog([plist objectForKey:@"message"]);
NSLog([plist objectForKey:@"number"]);
[plist setValue:textNumero.text forKey:@"number"];
NSLog([plist objectForKey:@"number"]);
[plist setValue:@"NO" forKey:@"firstTime"];
[plist writeToFile:localizedPath atomically:YES];
[self aggiorna];
[settingScreen removeFromSuperview];
}
Now I have a big problem, tha app works properly in all my developer device and in the simulator and the app reads and writes the file properly.
I submit the app on the Apple store but others user can't read/write this file.
Why is this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法写回应用程序包。您必须将原始 plist 文件复制到文档目录或任何其他可写位置,然后才能写入。
一个例子
You can't write back to the application bundle. You will have to copy the original plist file to the documents directory or any other writable location before it can be written to.
An example