sashoalm
sashoalm

Reputation: 79447

Get current user's home directory

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?

Upvotes: 22

Views: 17795

Answers (3)

Zack
Zack

Reputation: 1003

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 ~.

Upvotes: 4

zoul
zoul

Reputation: 104065

NSHomeDirectory: “Returns the path to the current user’s home directory.”

Example:

NSLog(@"Current user’s home directory is %@", NSHomeDirectory());

Upvotes: 49

just_a_guy
just_a_guy

Reputation: 53

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

Upvotes: 5

Related Questions