Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C documents directory keeps changing on device

I've been developing for iOS since the beginning and I've run into some trouble. I started making an app that needs to create files and save them to the documents folder and application support, however the good ol' standard way of using the

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *filePath = paths[0];

Doesn't work. It keeps bringing up a different file path every time the app is launched.

I've searched on google about this and everyone so far seems to just say they have this problem on the simulator. However, this is not a problem restricted to the simulator, it happens on the device every time the app is started, regardless of whether or not you run it from Xcode or not. Other search results just tell me the old way of doing it and/or are from too many years ago.

Does anyone know the current way to write to the Documents and Application Support folders is? This is a problem compiling from Xcode 6.1, using the iOS SDK8.1, running on both iOS7.1 and iOS8.1

Any help would be appreciated

like image 278
Hayden Avatar asked Nov 07 '14 18:11

Hayden


1 Answers

If you are storing paths in persistent storage with the intent to use that path at some date in the future, you should store only the path relative to the app's Documents folder and then whenever you need to access that resource, get the Documents folder path at runtime and append your relative path of your resource.

As an aside, I only see this changing sandbox path behavior when I run my app via Xcode. If I run the app directly from my device, the path is unchanged. Regardless, I'd always be inclined to use relative paths rather than fixed paths. I would never assume that the sandbox path couldn't change (e.g., if app is backed up and restored later, I wouldn't assume that the paths are unchanged).

like image 55
Rob Avatar answered Oct 14 '22 06:10

Rob