Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSHomeDirectory returns different path each time iPhone App is restarted

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.

like image 942
LearnCocos2D Avatar asked Jan 28 '10 12:01

LearnCocos2D


1 Answers

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.

like image 92
Ben Gottlieb Avatar answered Oct 26 '22 13:10

Ben Gottlieb