为什么 SwiftUI 中的 URL Scheme/onOpenURL 总是打开一个新窗口? [英] Why does URL Scheme/onOpenURL in SwiftUI always open a new window?

查看:43
本文介绍了为什么 SwiftUI 中的 URL Scheme/onOpenURL 总是打开一个新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将旧的 macOS 应用程序转换为 SwiftUI,但在使用新的 SwiftUI WindowGroup 时遇到了问题.

旧的应用程序是一个单窗口应用程序(基本上是一个美化的计时器),并且可以使用 URL 方案 (appname://15) 来更改计时器.

我尝试使用 onOpenURL 方法重新创建旧的 URL Scheme 功能,但是每当 URL Scheme 触发时,应用程序都会打开一个新窗口,我不知道如何阻止这种情况发生.

var body: some Scene {窗口组{内容视图().onOpenURL(执行:{ url inprint("\(url)")//这只是调试代码})}.命令{命令组(替换:.newItem,添加:{})}}

我不介意新版本的应用程序是否允许多个计时器,但 url 方案绝对不是为了每次使用时都打开新窗口.

如何阻止 onOpenURL 启动新窗口?我正在转换该应用程序专门用于学习 SwiftUI,但如果无法在 SwiftUI 中执行此行为,我愿意在某些 AppKit 代码中进行混合和匹配.

解决方案

文章,在 macOS 上的 SwiftUI 2.0 中打开窗口/场景" 展示了如何打开窗口.我把它的碎片放到了可以打开我的窗户而不是另一扇窗户的地方.

var body: some Scene {窗口组{内容视图().handlesExternalEvents(preferring: Set(arrayLiteral: "{path of URL?}"), allowed: Set(arrayLiteral: "*"))//如果存在,则激活现有窗口.onOpenURL(执行:{ url inprint("\(url)")//这只是调试代码})}.命令{命令组(替换:.newItem,添加:{})}.handlesExternalEvents(matching: Set(arrayLiteral: "{same path of URL?}"))//如果不存在则创建新窗口}

对于那些想要(尝试)更好地理解 preferringallowing 参数的人.转述自 Apple 文档:

preferring 参数是一组字符串,检查它们是否包含在此视图的 targetContentIdentifier 中(ContentView 在此case) 以查看此视图​​是否更喜欢处理外部事件(在本例中为 openURL)而不是另一个视图.

allowing 参数是一组字符串,检查它们是否包含在此视图的targetContentIdentifier 中,允许视图处理事件.>

空集永远不会匹配.*" 总是匹配的.

参考:关于 handlesExternalEvents 的 Apple 文档(首选:允许:)

I'm converting an old macOS app to SwiftUI, and I've run into a problem with the new SwiftUI WindowGroup.

The old app is a single window application (basically a glorified timer) and an URL scheme (appname://15) can be used to change the timer.

I've attempted to recreate the old URL Scheme functionality using the onOpenURL method, but whenever the URL Scheme triggers, the app opens a new window and I can't figure out how to stop that from happening.

var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL(perform: { url in
                    print("\(url)") // This is just debug code
                })
        }.commands {
            CommandGroup(replacing: .newItem, addition: { })
        }
    }

I don't mind if the new version of the app allows multiple timers, but the url scheme is definitely not intended to open up new windows every time it's used.

How do I stop onOpenURL from launching new windows? I'm converting the app specifically to learn SwiftUI, but if it's not possible to do this behavior in SwiftUI, I'm willing to mix and match in some AppKit code.

解决方案

The article, "Open window / scene in SwiftUI 2.0 on macOS" shows how to open windows. I took the pieces of it and made it to where it would simply open my window instead of a different one.

var body: some Scene {
 WindowGroup {
   ContentView()
     .handlesExternalEvents(preferring: Set(arrayLiteral: "{path of URL?}"), allowing: Set(arrayLiteral: "*")) // activate existing window if exists
     .onOpenURL(perform: { url in
         print("\(url)") // This is just debug code
      })
   }.commands {
      CommandGroup(replacing: .newItem, addition: { })
   }
   .handlesExternalEvents(matching: Set(arrayLiteral: "{same path of URL?}")) // create new window if doesn't exist
}

For those wanting to (try to) better understand preferring and allowing parameters. Paraphrased from Apple documentation:

preferring parameter is a Set of strings that are checked to see if they are contained in the targetContentIdentifier of this view (ContentView in this case) to see if this view prefers to handle the external event (openURL in this case) versus another view.

allowing parameter is a Set of strings that are checked to see if they are contained in the targetContentIdentifier of this view, allowing the view to handle the event.

Empty Sets are never matched. "*" are always matched.

Reference: Apple Documentation on handlesExternalEvents(preferring:allowing:)

这篇关于为什么 SwiftUI 中的 URL Scheme/onOpenURL 总是打开一个新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆