11

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

Hayden
  • 113
  • 1
  • 5

1 Answers1

19

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).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • True. I thought the code above is meant to get the right folder. I'm not storing the absolute path anywhere and this path changing happens regardless of wether or not I run through Xcode on a device – Hayden Nov 09 '14 at 11:14
  • If you're not storing the path anywhere, then I'm not understanding why you care if the path changed, as long as the file is still there. When I save a file to Documents and then run it again from Xcode, the path is different, but the file is still there. If you're not finding your file, first make sure that the file was successfully saved in the first place. – Rob Nov 09 '14 at 13:42
  • The file isn't there when I reload the app, thats why I'm confused. When I go to save the file I check its existence with NSFileManager's fileExistsAtPath:isDirectory: and the file exists and it's not a directory. While the app is still open I do the same and the file exists and isn't a directory. When I reload the app the file doesn't exist at all. If the file stayed between loads it'd be fine, but its not, even though it says it saved correctly – Hayden Nov 10 '14 at 10:22
  • Oh. I see the problem. The code was accidentally removing the file on launch. Sorry to waste everyone's time. – Hayden Nov 10 '14 at 10:30
  • @Hayden i am facing same problem. can you suggest me, how to resolve this problem – Sharda Prasad Oct 06 '17 at 12:16