Created
January 2, 2013 19:59
-
-
Save codeswimmer/4437418 to your computer and use it in GitHub Desktop.
iOS: How to access PList File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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