macos 如何在沙盒应用程序中获取用户主目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9553390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:56:40  来源:igfitidea点击:

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

objective-cmacoscocoaappstore-sandbox

提问by Justin808

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

NSHomeDirectory()正在重新调整我的沙箱根目录,而不是我的主目录。[@"~" stringByExpandingTildeInPath]正在做同样的事情。

This /Users/username/Library/Containers/appID/Datais what's being returned. How do I get /Users/username/?

/Users/username/Library/Containers/appID/Data就是返回的内容。我如何获得/Users/username/

回答by CRD

If you want the path to the user's real home directory you can use:

如果您想要用户真实主目录的路径,您可以使用:

char *realHome = getpwuid(getuid())->pw_dir;

Full example:

完整示例:

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <assert.h>

NSString *RealHomeDirectory() {
    struct passwd *pw = getpwuid(getuid());
    assert(pw);
    return [NSString stringWithUTF8String:pw->pw_dir];
}

This gives you the pathto the user's home, but does not automatically give you accessto that folder. As noted in comments, you can use this path for:

这为您提供了用户主目录的路径,但不会自动为您提供对该文件夹的访问权限。如评论中所述,您可以将此路径用于:

  • providing a sane default folder for the open/save dialogs
  • detecting whether you are in a sandbox, by comparing the result to NSHomeDirectory()
  • 为打开/保存对话框提供一个合理的默认文件夹
  • 通过将结果与 NSHomeDirectory()

回答by Parag Bafna

From apple documentation:

来自苹果文档

Accessing User Data

Mac OS X path-finding APIs, above the POSIX layer, return paths relative to the container instead of relative to the user's home directory. If your app, before you sandbox it, accesses locations in the user's actual home directory (~) and you are using Cocoa or Core Foundation APIs, then, after you enable sandboxing, your path-finding code automatically uses your app's container instead.

For first launch of your sandboxed app, Mac OS X automatically migrates your app's main preferences file. If your app uses additional support files, perform a one-time migration of those files to the container, as described in “Migrating an App to a Sandbox.”

If you are using a POSIX command such as getpwuid to obtain the path to the user's actual home directory, consider instead using a Cocoa or Core Foundation symbol such as the NSHomeDirectory function.By using Cocoa or Core Foundation, you support the App Sandbox restriction against directly accessing the user's home directory.

If your app requires access to the user's home directory in order to function, let Apple know about your needs using the Apple bug reporting system.

访问用户数据

Mac OS X 路径查找 API,在 POSIX 层之上,返回相对于容器而不是相对于用户主目录的路径。如果您的应用在沙盒之前访问用户实际主目录 (~) 中的位置,并且您使用的是 Cocoa 或 Core Foundation API,那么在启用沙盒后,您的寻路代码会自动使用应用的容器。

首次启动沙盒应用程序时,Mac OS X 会自动迁移应用程序的主要首选项文件。如果您的应用程序使用其他支持文件,请将这些文件一次性迁移到容器,如“将应用程序迁移到沙箱”中所述。

如果您使用 POSIX 命令(例如 getpwuid)获取用户实际主目录的路径,请考虑使用 Cocoa 或 Core Foundation 符号(例如 NSHomeDirectory 函数)。通过使用 Cocoa 或 Core Foundation,您支持应用沙盒限制直接访问用户的主目录。

如果您的应用程序需要访问用户的主目录才能运行,请使用Apple 错误报告系统让 Apple 了解您的需求。

回答by Robin Schmidt

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

使用 Swift,您可以从沙盒应用程序访问用户主目录路径,如下所示:

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/


我根据这篇博文中的一个片段创建了这个解决方案:http: //zpasternack.org/accessing-the-real-home-folder-from-a-sandboxed-app/