首页 > 解决方案 > 如何在 macOS 上找到/注册 url 方案和文件类型处理程序?

问题描述

我正在用 Objective-C 为第三方 macOS 应用程序创建一个插件。我想注册和处理 url 方案(如myscheme://somedatahere)和文件类型 - 例如,当用户双击文件时,我们的插件应该会收到通知。

由于我的产品只是第三方应用程序的插件,因此代码无法访问应用程序的 NSApplication 实例,也无法访问其事件。因此,我需要创建一个帮助应用程序并将其捆绑在插件包/包中以进行注册和处理。如果这些假设不正确,请告诉我。

插件包安装在Library/Application Support(用户通常将插件下载到 Downloads 文件夹中,双击它,然后主机应用程序的处理程序将插件复制到 Application Support 文件夹中)。应用程序处理 url 和文件类型将放置在插件包中。该位置由主机应用程序选择。

我有几个不确定性我无法找到答案:

标签: objective-cmacos

解决方案


您可以在Launch Services文档中找到您正在寻找的答案。

如果您检查“应用程序注册”部分,您将看到:

一个内置的后台工具,在系统启动或新用户登录时运行,自动搜索系统、网络、本地和用户域中的应用程序文件夹,并注册在那里找到的任何新应用程序。

Finder 会在发现所有应用程序时自动注册它们,例如当它们被拖到用户的磁盘上或当用户导航到包含它们的文件夹时。

因此,如果您的帮助应用程序通过用户手动拖放安装到 /Library/Application Support 中,看起来启动服务会看到它并注册 URL 方案。

否则,文档将继续涵盖手动案例:

In spite of these automatic registration utilities, it may sometimes be necessary to register an application explicitly with Launch Services. For example, although developers are encouraged to package their applications so that they can be installed by simply dragging them onto the user’s disk, some applications may require more elaborate custom installer software. In such cases, the installer should call one of the Launch Services registration functions LSRegisterFSRef or LSRegisterURL to register the application explicitly.


推荐阅读