You may have noticed that the iPhone Simulator has changed with Xcode 6, and with it – of course – the path to your simulated apps’ Documents Directory. At times we may need to take a look at it.
Finding that path is not as easy as it was once, namely Library/Application Support/iPhone Simulator/7.1/Applications/ followed by a cryptic number representing your app.
As of Xcode 6 and iOS 8 you’ll find it here:
Library/Developer/CoreSimulator/Devices/cryptic number/data/Containers/Data/Application/cryptic number
It was fairly easy to spot that one cryptic number before – but now you have to play the guessing game twice. This can become tedious, especially if you’re testing several user accounts and therefore accumulate a vast array of cryptic numbers.
Here’s a great way of writing out the Documents directory as a log message:
// where are you?
NSLog(@"Documents Directory: %@", [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
Add this to a viewDidLoad or applicationDidFinishLaunching method and you’re golden. If you’re running the app on a Simulator, this will write out the correct location for the current app.
Thanks to Ian for this tip!
Worse yet: every time you run the app, the folder location changes!
Thanks to Keith for bringing this up:
The location of this folder changes every time you run the app on the Simulator. Only Apple know why.
The good news is that once you open a Finder window and navigate to your project, Finder will update the location automatically as long as you don’t close that window.
At least this way you don’t have to play detective every time you launch the app from Xcode.
Complex and Cumbersome Method
Originally I did the following to detect where they Documents directory was:
- add an identifiable file to your bundle (for example, a picture)
- copy that file into your app’s Documents directory, using a name you can remember
- run the app on the simulator
- search for the file on the command line
Here’s how I did this:
// let's save a file that we know
NSURL *resource = [[NSBundle mainBundle]URLForResource:@"swift-icon" withExtension:@"png"];
NSURL *destination = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]URLByAppendingPathComponent:@"WhereAreYouHiding-4s"];
[[NSFileManager defaultManager]copyItemAtURL:resource toURL:destination error:nil];
This will copy a hypothetical file “swift-icon.png” from the Main Bundle to the simulator’s Documents directory as “WhereAreYouHiding-4s”. I’m adding 4s here because I’d like to find that particular simulator’s directory – which is likely different for the other models.
Searching for this file in Finder won’t yield any results – because a standard user does not have the permissions to read the simulator’s directory. Hence we’ll open Terminal and look for our file with root privileges:
sudo find / -name 'WhereAreYou*'
This will prompt you to type in your Administrator password for your Mac and will go to work. The find command will look through every file on your hard disk and should only come up with very few results. Once you’ve found something resembling that cryptic path I’ve mentioned above, you can stop the search with CTRL+C.