Get current user's home directory

前端 未结 3 901
终归单人心
终归单人心 2021-02-18 23:50

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/

3条回答
  •  面向向阳花
    2021-02-19 00:49

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

提交回复
热议问题