Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is NSHomeDirectory? When it is used?

Tags:

ios

I want to understand the two commonly used, NSDocumentDirectory which I have used in NSSearchPathForDirectoriesInDomains. The other one is NSHomeDirectory. What is the difference between these two?

like image 753
Kumar Utsav Avatar asked Oct 19 '22 19:10

Kumar Utsav


1 Answers

NSHomeDirectory() returns the root directory for your sandboxed app on iOS, but it's not recommended to use this.

If you want to read and write files, the most common places to use are the documents directory and the caches directory. Use the former for things you want to save for a long period of time, and the latter for things you want to save only temporarily. Apple provides specific ways to find these two directories rather than trying to access them through the home directory, so you should use it: NSSearchPathForDirectoriesInDomains().

Lots of tutorials use NSHomeDirectory, and it wouldn't exactly be hard for Apple to remove the function from iOS if they really wanted it dead. However, in practice you should use the NSSearchPathForDirectoriesInDomains() function to locate standard directories like Documents and Library/Caches – they could be moved anywhere at any time in the future, so it's sensible to ask iOS where they are rather than try to hard-code paths.

In summary, here's a comment I posted to the original question: "I'm curious you think it's common to use NSHomeDirectory() on iOS. It really isn't – you should request the path to the subfolder you want, e.g. documents or caches." The function exists, and many people use it, but I would strongly suggest you do not.

like image 76
TwoStraws Avatar answered Nov 18 '22 02:11

TwoStraws