【问题标题】:Deep link into macOS app is not recognized无法识别到 macOS 应用程序的深层链接
【发布时间】:2017-03-31 16:56:35
【问题描述】:

我正在尝试在 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>

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

我在这里做错了什么?

【问题讨论】:

  • 我自己从来没有做过这个特别的事情,但是这个答案的一部分似乎很有用:stackoverflow.com/a/49512/7419656
  • @Ssswift thx 伙计 - 这行得通!至少打开链接时我得到了一个“hello world”:D

标签: swift macos hyperlink deep-linking


【解决方案1】:

感谢@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);

}

【讨论】:

  • 我知道这是 4 年前的事,但想知道您是否可以分享您的 .plist
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
相关资源
最近更新 更多