How do I get the users home directory in a sandboxed app?

前端 未结 3 1822
[愿得一人]
[愿得一人] 2020-12-09 02:49

NSHomeDirectory() is retuning my sandbox root, not my home directory. [@\"~\" stringByExpandingTildeInPath] is doing the same thing.

This <

3条回答
  •  我在风中等你
    2020-12-09 03:33

    Using Swift, you can access the users home directory path from a sandboxed app like this:

    var userHomeDirectoryPath : String {
        let pw = getpwuid(getuid())
        let home = pw?.pointee.pw_dir
        let homePath = FileManager.default.string(withFileSystemRepresentation: home!, length: Int(strlen(home)))
    
        return homePath
    }
    


    I created this solution based on a snippet in this blog post: http://zpasternack.org/accessing-the-real-home-folder-from-a-sandboxed-app/

提交回复
热议问题