In a recent application I needed to create several property list files (plists) to store application information. I opted to store the files in the folder named “Documents” off the home folder.
Here is the code I used to specify the path where I wanted the files written and the code to build an NSString with the full path.
1 2 3 4 5 6 7 8 9 10 11 12
// Folder and file name #define USER_PLIST_FULLPATH @"/Documents/User.plist" // Path to the user property list NSString *plistPath; plistPath = [NSHomeDirectory() stringByAppendingString:USER_PLIST_FULLPATH]; // Check for existence of the file if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) ... else ...
The key to making this work is NSHomeDirectory() a low level C function that returns the path to the current user’s home directory. See the document Low-Level File Management Programming Topics for the specifics on working with the file system.
In the next post I’ll show another means to generate paths to files, arguably a better approach as it uses the provided frameworks for building paths (versus hardcoding as shown here).
Important Note: There is a /Documents directory created in the sandbox for each iPhone application. In other words, the plist file that I created above will not clash with a file of the same name in another iPhone application.