Kapil
Kapil

Reputation: 108

updating child plist programittically shown in simulator but not in device

I am implementing setting bundle first time and I have done it by creating Root.plist and Child.plist in Settings.bundle and have some fields in these plist. I need to update the fields (add or remove) of Child.plist according to web services and I have written code for that in appdelegate. Its working fine in simulator (all fields are updated accordingly) but its not working in device. Can any one suggest me what could be the reason of it.

code to copy plist to document directory and update that (as per the 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];

Upvotes: 0

Views: 165

Answers (1)

Jordan Montel
Jordan Montel

Reputation: 8247

I think you have a problem to write your PLIST. You need to copy your PLIST from your resources bundle into the documents directory and modify the PLIST inside this folder.

For that you can use this piece of code :

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];
}

Have a look here.

Upvotes: 1

Related Questions