Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

ios - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...