mac 上应用程序及其小部件之间共享文件的首选位置

Preferred location for file shared between application and it's widget on mac

我也尝试向其中添加小部件的应用程序。 我想知道我应该把我想在应用程序和小部件之间使用的文件放在哪里(sql 核心数据的精简数据库)。

目前应用数据在里面: ~/Library/Application Support/my.app/

小部件数据在里面: ~/Library/Application Support/my.app.widget/

两者都启用了沙盒(根据需要)。 我希望可以从应用程序和小部件访问 sql 文件。

根据"The Application Group Container Directory" section of Apple's App Sandbox Design Guide

an application can use the com.apple.security.application-groups entitlement to request access to a shared container that is common to multiple applications produced by the same development team. This container is intended for content that is not user-facing, such as shared caches or databases.

这意味着您可以通过使用 "containerURLForSecurityApplicationGroupIdentifier" method of NSFileManager 获取应用程序和小部件的共享数据文件夹的路径来获取应用程序组容器的路径。