开发者

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开发者_Python百科 "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.


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.


The GUID portion of an app's home directory appears to be just a random UUID (as indicated by the 3rd group of hex digits starting with a '4' and the 4th group starting with '8','9','A' or 'B'. See the wikipedia entry on 'universally unique identifier' as a good starting place if you want to learn more about UUIDs and unique IDs in general. The reason for using a unique id in the directory path is just to separate one app (and multiple deploments of the same app) from one another as part of sandboxing (IMHO).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜