gpt4 book ai didi

objective-c - 读写plist文件的奇怪问题

转载 作者:行者123 更新时间:2023-12-03 20:21:07 26 4
gpt4 key购买 nike

我有一个从 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 商店提交了应用程序,但其他用户无法读取/写入此文件。

这是为什么?

最佳答案

您无法写回应用程序包。您必须将原始 plist 文件复制到文档目录或任何其他可写位置,然后才能写入。

示例

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString libraryPath = [paths objectAtIndex:0];
NSString plistPath = [libraryPath stringByAppendingPathComponent:@"settings.plist"];

// Checks if the file exists at the writable location.
if ( ![[NSFileManager defaultManager] fileExistsAtPath:plistPath] ) {
NSString *masterFilePath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];

// Try to copy the master file to the writable location
NSError *error;
if ( ![[NSFileManager defaultManager] copyItemAtPath:masterFilePath toPath:plistPath error:&error] ) {
NSLog(@"Error: %@", [error localizedDescription]);
// Serious error.
}
}

...
// Ready for use.
NSData *plistData = [NSData dataWithContentsOfFile:plistPath];

关于objective-c - 读写plist文件的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6193912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com