ios - 以编程方式更新在模拟器中显示但不在设备中显示的子 plist

标签 ios settings.bundle

我第一次实现设置包,我是通过在 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];

最佳答案

我认为您在编写 PLIST 时遇到问题。您需要将资源包中的 PLIST 复制到文档目录中,并修改此文件夹中的 PLIST。

为此,您可以使用这段代码:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”];
     [fileManager copyItemAtPath:bundle toPath: path error:&error];
}

看看here .

关于ios - 以编程方式更新在模拟器中显示但不在设备中显示的子 plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19403552/

相关文章:

iphone - iOS - 在 UIScrollView 上缩放 UIImageView

iphone - 设置 UIButton 字体

ios - 如何生成iOS代码以绘制图像?

ios - 识别哪些字段在 CoreData 保存之前发生了变化

iphone - 分层 iPhone 设置文件问题 -- 无法添加到 Settings.bundle

objective-c - iPhone 应用程序的设置包

ios - 对 info.plist 中定义的深层链接没有反应

ios - 没有设置捆绑 IOS 的设置条目

iphone - 在设置捆绑 iOS 时关闭键盘/数字键盘

ios - InAppSettingsKit 具有多种本地化版本