从 iOS9 开始,通用链接可以在 iOS 上使用,但我想知道 macOS 上是否有类似的东西?我们有一个应用程序,它是带有相应 Web 应用程序的 macOS 应用程序,我们希望用户单击链接在 Mac 应用程序中打开,而不是使用目标链接打开浏览器。
这在 macOS 上有可能吗?我刚刚找到了 iOS 的示例,但没有找到 macOS 的示例?!?
在 macOS 上支持通用链接需要您的应用程序和网站(以及 macOS 10.15+)的支持。
请参阅苹果的文档允许应用程序和网站链接到您的内容 https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content了解详细信息(本文涉及 iOS、tvOS 和 macOS 深层链接)及其子文章启用通用链接 https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/enabling_universal_links也处理通用链接 https://developer.apple.com/documentation/uikit/inter-process_communication/allowing_apps_and_websites_to_link_to_your_content/handling_universal_links.
快速概述:
基本上,它与 iOS 上的相同:您需要创建一个apple-app-site-association
文件托管在您的服务器的根目录或.well-known
目录(描述于“启用通用链接”上面链接)。它描述了哪些 URL 路径可以传递到您的应用程序。重要的是,该文件是有效的 JSON(验证它!),并且通过 HTTPS 使用有效证书提供服务,并且没有任何重定向!
您需要在配置文件中启用“关联域”功能。详细信息取决于 Xcode 是否管理您的配置文件。在您的权利文件(通常可通过 Xcode 中目标的“签名和功能”选项卡进行编辑)中,您需要添加相应的关联域,例如applinks:my.domain.example
(no https://
或任何东西,只是原始域名)。
然后你需要实施application(_:continue:restorationHandler:) https://developer.apple.com/documentation/appkit/nsapplicationdelegate/1428471-application在您的应用程序委托中处理通用链接。
在开发过程中让 macOS 第一次获取关联可能会很烦人。调试提示:
- Open
Console.app
并过滤swcd
,这是负责关联 Web 凭据和通用链接的守护进程。有时您会看到为什么它不起作用的提示(例如拒绝apple-app-site-association
file).
- Also I had to kill the
swcd
daemon to make it get restarted by launchd
and then pick up changes/fixes I did to the apple-app-site-association
file. Both iOS and macOS should usually reload this file on "app installation"1 (haven't seen this getting specified more clearly; I guess they mean App Store installs/updates but I don't know about when this is loaded for macOS apps distributed outside the App Store).
- Also, be aware that entering a Universal Link in Safari will not hand off to your app: Apple has stated in the dev forums2 that they believe if a user is already in the browser environment they want to stay there and not suddenly move to an app. Therefor, to test the Universal Link, open
Notes.app
, create a new note and enter/paste the Universal Link you want to test there. You can then click it and get asked whether you want to open this in your app.
1)抱歉,没有我阅读本文的开发论坛链接。
2) 再次抱歉,目前没有链接。苹果员工至少两次提到过这一点,都引用了Notes
诡计。尽管他们在那里谈论的是 iOS,但这种行为在 macOS 上也是一样的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)