Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode changes location of applications folder on build

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
like image 872
user3245597 Avatar asked Feb 22 '15 00:02

user3245597


People also ask

Where are Xcode files stored?

It should by located in: ~/Library/Developer/Xcode/DerivedData . Save this answer. Show activity on this post. You can configure the output directory using the CONFIGURATION_BUILD_DIR environment variable.

What is a target in Xcode?

A Target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. An Xcode scheme defines a collection of targets to build, a configuration to use when building, and a collection of tests to execute.

How to open folder with Xcode?

Click Open on the Welcome screen or File | Open from the main menu. Navigate to the desired . xcodeproj directory.


2 Answers

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.

like image 63
Cihan Tek Avatar answered Sep 20 '22 09:09

Cihan Tek


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
}
like image 34
subCipher Avatar answered Sep 23 '22 09:09

subCipher