views:

978

answers:

1

I noticed that NSHomeDirectory returns a different path each time i restart the App with Xcode, and apparently even if i click the icon manually since it doesn't load the file's contents. I'm stunned that it gives me a different directory each time i restart the app. This happens on both simulator and device and even if i use the "ForUser" method. My calls look like this:

NSString* fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"test.file"];

or

NSString* fullPath = [NSHomeDirectoryForUser(@"MrX") stringByAppendingPathComponent:@"test.file"];

or even

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"test.file"];

And the log of two app starts looks like this, notice how the GUID part has changed:

loadCharacters: /var/mobile/Applications/ABE7E33E-439B-4258-8FC1-127A3CD00D87/test.file
loadCharacters: /var/mobile/Applications/71C02507-6347-4693-8CC1-537BE223179E/test.file

What am i doing wrong, or what am i missing?

SOLUTION:

At first i was saving to NSHomeDirectory itself, not the "Documents" subdirectory. Since the App directory changes with each deployment but only the files in the "Documents" directory get copied, the saved file was lost after each deployment. Then i used the "Documents" folder but someone suggested i should probably put it in a subfolder of "Documents" i did just that but forgot to create the folder, hence the file wasn't saved. I double-failed on this one. :)

Simply saving to NSHomeDirectory() + "Documents/test.file" works.

+7  A: 

You're doing this correctly. Each time an app is installed into the simulator or device, it's placed into a different directory (I'm not sure why, but that's what happens). However, all of its directory structure is moved to the new position. Thus, your RELATIVE paths will remain unchanged.

Ben Gottlieb
how can i test save/load this way then?
GamingHorror
By using the code you posted above to provide the path
James Raybould
That's true, i know it works since i do a load after each save. However, the different paths still appear when i build an ad-hoc distributed version. How can i prove that save/load works if it doesn't work for the testers due to this issue? In other words: my testers aren't able to verify a working save/load system.
GamingHorror
What do you mean by save/load? All your saved data should still be there for loading the next time you install.
Ben Gottlieb
It may be there but the path to the files has changed. So how can i access it? Or is that data that should automatically be copied with each deployment by the iPhone SDK / Xcode? If so, then that's not happening.
GamingHorror
Awwwww ... got it! I was trying to save to a subfolder of Documents that didn't exist. Saving directly to Documents works. Data is copied over correctly.
GamingHorror
Feel free to create a directory structire in Documents if that is what you need.
St3fan