gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:14:14 24 4
gpt4 key购买 nike

我正在尝试实现到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com