Xcode changes location of applications folder on build

情到浓时终转凉″ 提交于 2019-11-29 06:46:50
Cihan Tek

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!