I am storing an .mp3 in my iOS apps documents directory and saving the path to Core Data. When I rebuild the app Xcode seems to move the application data to a different directory thus making the stored path invalid. Why is this happening and what are the best practices around saving file paths?
I am using Xcode 6.2 and have been successful retrieving the file in the past.
location of file after downloading:
in ~/Library/Developer/CoreSimulator/Devices/82D1931C-590D-45A2-AB9B-8D1D4F2530C5/data/Containers/Data/Application/
$ find . -iname p612.mp3
./1C070BF8-0E2E-4EAC-99B6-C56E48675E6E/Documents/adf07cf85254e8f28f942f2d6fa704ae/p612.mp3
Location of file after rebuilding:
in ~/Library/Developer/CoreSimulator/Devices/82D1931C-590D-45A2-AB9B-8D1D4F2530C5/data/Containers/Data/Application/
$ find . -iname p612.mp3
./E6C7D0AF-E61C-4BDD-AF4B-68C445E2BB0D/Documents/adf07cf85254e8f28f942f2d6fa704ae/p612.mp3
Since iOS 8.0, every time you rebuild your code, the name of the application folder (which contains your documents and library directories) changes. This is the intended behaviour.
So if you want to store a path, you should only store the filename, and then combine it with the location of the documents directory on the fly. This way, it will always point to the correct location.
The contents of the documents directory will persist even though the name of the folder in which it resides will change from build to build.
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
See: Technical Note TN2406.
Swift update for returning the DocumentsDirectory for persistence between builds. This answer will return the path to the Dir. You can then append the file name / last component to complete.
func findAppDir()->String{
let appDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let path = "file://\(appDir)/"
return path
}