获取 OS X 中用户库文件夹的路径

发布于 2024-12-13 23:57:10 字数 335 浏览 8 评论 0原文

我需要打开一个 NSSavePanel,并将用户库文件夹作为目标文件夹。通常,我会通过在 [NSSavePanel beginSheetForDirectory] 中输入 ~/Library/ 来完成此操作。

只要应用程序没有被沙箱化,这种方法就可以正常工作。对于沙盒应用程序,这将导致 NSSavePanel 尝试访问应用程序文档“盒子”内的文件夹。

我无法引用 /Users/username/Library/ 因为我在运行时不知道用户username。那么我如何链接到可可中的这条路径呢?

I need to open a NSSavePanel with the users Library folder as destination folder. Normally I would do this by entering ~/Library/ in [NSSavePanel beginSheetForDirectory].

This works fine as long as the application is not sandboxed. For sandboxed applications this will result in the NSSavePanel trying to access a folder inside the applications document "box".

I cannot refer to /Users/username/Library/ as I do not know the users username at runtime. So how do I link to this path in cocoa?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-12-20 23:57:10

我不确定沙箱如何适应这一点,但您可以使用以下命令找到用户的库目录:

NSArray* paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES );

I'm not sure how sandboxing fits in with this, but you can find the user's library directory using:

NSArray* paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES );
洋洋洒洒 2024-12-20 23:57:10

不确定这是否适用于沙盒应用程序,但这就是我现在的做法。这将返回 /User/他们的用户名

-(NSString *)homeDirectory
{
    return NSHomeDirectory();
}

Not sure if this will work on a sandboxed application but this is how I do it right now. This will return /User/TheirUserName

-(NSString *)homeDirectory
{
    return NSHomeDirectory();
}
凉墨 2024-12-20 23:57:10

这取决于您想要实现的目标。

It depends what you are trying to achieve.

  • If the behavior is required by your application, then you can request a temporary exception entitlement when submitting the application to the Mac App Store. But sooner or later, you will have to find a solution to remove this exception.
  • If you want to access data that were previously stored in the ~/Library/ folder, you can define a migration strategy to move back the data into the sandbox.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文