Get current user's home directory

前端 未结 3 893
终归单人心
终归单人心 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:37

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

    0 讨论(0)
  • 2021-02-19 00:43

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

    Example:

    NSLog(@"Current user’s home directory is %@", NSHomeDirectory());
    
    0 讨论(0)
  • 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 ~.

    0 讨论(0)
提交回复
热议问题