Friday, August 6, 2010

Copy-Paste Code: Opening up Settings.plist

So you've bundled a Settings.plist, and now you want to know why you can't edit it? It's because it's not in a writable directory. Let's put that baby where she belongs:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

settingsPlistPath = [[documentsDirectory stringByAppendingPathComponent:@"Settings.plist"] copy];



NSFileManager *fileManager = [[NSFileManager alloc] init];



if (![fileManager fileExistsAtPath:settingsPlistPath]) {

    NSString *bundledSettings = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];

    [fileManager copyItemAtPath:bundledSettings toPath:settingsPlistPath error:NULL];

}



self.settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPlistPath];

No comments:

Post a Comment