How do I load a plist file from disk as a NSDictionary on iOS?

sorin picture sorin · Mar 18, 2011 · Viewed 25.1k times · Source

I want to load the plist file from disk (documents, application cache, ...) not from a resource bundle.

Answer

Lachlan Roche picture Lachlan Roche · Mar 18, 2011

You can load a plist from any accessible file path with -initWithContentsOfFile: or +dictionaryWithContentsOfFile:

Load a plist from a file, and create the file if it did not exist:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                    NSUserDomainMask, YES); 
self.plistFile = [[paths objectAtIndex:0]
                    stringByAppendingPathComponent:@"example.plist"];

self.plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile];
if (!plist) {
    self.plist = [NSMutableDictionary new];
    [plist writeToFile:plistFile atomically:YES];
}