我第一次实现设置包,我通过在 Settings.bundle 中创建 Root.plist 和 Child.plist 并在这些 plist 中有一些字段来完成它。我需要根据 Web 服务更新 Child.plist 的字段(添加或删除),我已经在 appdelegate 中为此编写了代码。它在模拟器中工作正常(所有字段都相应更新)但它在设备中不工作。任何人都可以建议我这可能是什么原因。
将 plist 复制到文档目录并更新它的代码(根据 asnwer)
NSString *bundle = [[NSBundle mainBundle]pathForResource:@"Settings" ofType:@"bundle"];
NSString *path = [bundle stringByAppendingPathComponent:@"Category.plist"];
NSMutableDictionary *dictOfPlist = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableArray *dictArray = [dictOfPlist valueForKey:@"PreferenceSpecifiers"];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path1 = [documentsDirectory stringByAppendingPathComponent:@"Child.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path1])
{
[fileManager copyItemAtPath:path toPath: path1 error:&error];
}
for (some condition)
{
// creating dictionary
// add dictionary to array dictArray
}
[dictOfPlist setObject:dictArray forKey:@"PreferenceSpecifiers"];
[dictOfPlist writeToFile:path1 atomically:YES];