Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created January 2, 2013 19:59
Show Gist options
  • Select an option

  • Save codeswimmer/4437418 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/4437418 to your computer and use it in GitHub Desktop.
iOS: How to access PList File
Check first if PLIST exist
NSArray *myPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [myPath objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"%@.plist", plistName] ];
[myPlistPath retain];
// If it's not there, copy it from the bundle
NSFileManager *fileManger = [NSFileManager defaultManager];
if ( ![fileManger fileExistsAtPath:myPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
}
Read PLIST
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"myApp.plist"];
NSMutableDictionary *plist = [NSDictionary dictionaryWithContentsOfFile: path];
Read and set
NSString *value1 = (NSString *)[plist valueForKey:@"key1"];
NSString *value2 = (NSString *)[plist valueForKey:@"key2"];
[plist setValue:@"zaldzbugz" forKey:@"key1"];
[plist writeToFile:path atomically:YES];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment