Get current user's home directory

Viewed 16673

I'm developing a Cocoa Objective-C application that will run on Mac OS X. I need to get the full path of the current user's home directory:

/Users/MyUser/

Is there a standard function that does that in Objective-C/Cocoa?

3 Answers

Since macOS 10.12 you can also use FileManager.default.homeDirectoryForCurrentUser, see the documentation.

You can use FileManager.default.homeDirectoryForCurrentUser -> URL or NSHomeDirectory() -> String - they both return the same thing, just in different types.

There's an important caveat, though:

In macOS, it is the application’s sandbox directory or the current user’s home directory (if the application is not in a sandbox).

If you're building your app to be distributed in the App Store, you're going to have to deal with the fact that these functions return the sandboxed home and not the directory the user thinks of as ~.

Related