chippydip wrote:
For some unknown reason, the default directory in the simulator is your actual machine's root folder (yes, you can access your entire filesystem from the simulator).
Currently the simulator doesn't actually run the applications in a sandbox. However, the exact nature of the sandbox is not set in stone for all time, as far as I'm aware. Currently, the sandbox includes a chroot() to the app's 'home folder', but that might not always be the case-- it could conceivably be changed at some point such that apps signed by the same signature, or distributed by the same vendor, would have access to one another. Unlikely, perhaps, but possible.
As such, the correct behavior is
not to rely on the sandbox to chroot() you to a reasonable app-specific place. What you ought to do is make judicious use of a couple of useful path routines, which behave identically on the device and in the simulator.
1.
NSHomeDirectory()
This will return the user's home folder. At present, this returns the path to the application sandbox folder (the GUID one).
2.
NSSearchPathForDirectoriesInDomains()
This is the better one, and the most future-proof, localizable, etc, etc. You call something like:
NSArray * list = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
…and you'll get a list of folders back. The second argument is actually a bitfield, designed so you could look in the ApplicationSupport folder for User, Local, and Network domains to search inside ~/Library, /Library, and /Network/Library. Usually when using NSUserDomainMask you can safely just use [list lastObject] to get the single path it returns, then use that to store your data.
Look in
<Foundation/NSPathUtilities.h> for more information and more folders for which you can search. Note that the implementation of this function will track any changes in the folder layout schema made my Apple, so is better than constructing a path yourself based on either the root or the output of NSHomeDirectory().