swift - 无法识别到 macOS 应用程序的深层链接

标签 swift macos hyperlink deep-linking

我正在尝试实现到 macOS 应用程序的深层链接,但似乎没有任何效果。

到目前为止,我的 AppDelegate.swift 包含以下内容

func application(app: NSApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    print("opened by link");
    return true
}

我还配置了 info.plist,其中 URLSchemes 是我的包标识符,而 URLIdentifier 只是 zst

在一个简单的 html 文件中,我使用以下代码来测试深层链接

<a href="zst://test/link">Test</a>

我的应用程序已打开(或在已运行时变为事件状态),但未执行打印语句。

我在这里做错了什么?

最佳答案

感谢@Ssswift,我找到了解决方案。 使用此代码: How do you set your Cocoa application as the default web browser?

并将其转换为 swift 格式:https://objectivec2swift.com

适用于 Swift 3

AppDelegate.swift 中添加了这些行

func applicationDidFinishLaunching(_ aNotification: Notification) {
    var em = NSAppleEventManager.shared()
    em.setEventHandler(self, andSelector: #selector(self.getUrl(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}

func getUrl(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {
    // Get the URL
    var urlStr: String = event.paramDescriptor(forKeyword: keyDirectObject)!.stringValue!
    print(urlStr);

}

关于swift - 无法识别到 macOS 应用程序的深层链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146004/

相关文章:

xcode - 如何创建 OpenCV 框架?

ios - 重复目标无法找到 Storyboard 中引用的类 - 原始目标可以。

ios - xCode 中的自动布局未按预期运行

ios - 尝试用 GCD 创建一个准确的计时器

java - OS X El Capitan 上的 Eclipse 错误 : Could not find or load main class

html - 是否有仅针对 Mac 上的 Chrome 的 CSS hack?

ios - 播放大量音频文件后,AVAudioPlayer停止播放:kAudio_TooManyFilesOpenError

html - 字体、背景和页脚有问题

html - 链接到外部 css 文件

jquery - 使用 jQuery 使表格行 (tr) 可点击(使用 a href 链接,然后悬停!?)