我如何获得“/library/containers”的路径/&quot目录

发布于 2025-01-21 18:12:41 字数 727 浏览 1 评论 0原文

一个沙盒Macos应用程序可以轻松访问两个文件夹中的数据:

  1. 〜/library/containser/programName/
  2. 〜/library/group Containser/securityGroupname/

以下代码将为我提供“〜/library/group Container/”路径

let fileManager = FileManager.default
let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "myGroup")
print("path:",directory)

...将打印:

path:/users/myName/library/group Containers/myGroup

我想获得“〜/library/容器/”目录,而不是“〜/library/组集装箱” /“目录。

这必须很容易做到,因为它是一个通用目录。

有人知道如何使用Swift功能执行此操作吗?

我知道我可以用它来创建它:

var path: String = "/Users/etc/Containers/" + programName

A sandboxed macOS app can easily access data in two folders:

  1. ~/Library/Containers/ProgramName/
  2. ~/Library/Group Containers/SecurityGroupName/

The following code will give me the "~/Library/Group Container/" path ...

let fileManager = FileManager.default
let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "myGroup")
print("path:",directory)

This will print:

path: /Users/myName/Library/Group Containers/myGroup

I want to get the path of the "~/Library/Containers/" directory, not the "~/Library/Group Containers/" directory.

This must be easy to do, as it is a common directory.

Does anybody know how to do this using a Swift function?

I know I can just create it with:

var path: String = "/Users/etc/Containers/" + programName

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

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

发布评论

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

评论(1

嘴硬脾气大 2025-01-28 18:12:41

在沙箱中,API

let url = try FileManager.default.url(for: .libraryDirectory,
                                      in: .userDomainMask,
                                      appropriateFor: nil,
                                      create: false)

指向当前应用程序容器的用户库。

file:///Users/myself/Library/Containers/com.myself.Greatapp/Data/Library/

您无法访问更高级别的文件夹〜/library/Containers

更新:

在iOS 16+中,Macos 13+有一个快捷方式,它将URL返回到当前用户的文件夹。如果将应用程序打包给应用程序容器中的文件夹,则将URL点点为文件夹中的文件夹中的文件夹中的文件夹中的文件夹

let url = URL.libraryDirectory

In the sandbox the API

let url = try FileManager.default.url(for: .libraryDirectory,
                                      in: .userDomainMask,
                                      appropriateFor: nil,
                                      create: false)

points to the user library of the container of the current app.

file:///Users/myself/Library/Containers/com.myself.Greatapp/Data/Library/

You don't have access to the higher level folder ~/Library/Containers

Update:

In iOS 16+, macOS 13+ there is a shortcut, it returns the URL to the Library folder of the current user. If the app is sandboxed the URL points to folder in the application container otherwise to the folder in the home folder

let url = URL.libraryDirectory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文