SwiftUI apps in macOS are not triggering onOpenURL when receiving a Universal Link

Hi
I have a simple SwiftUI app with an onOpenURL handler to handle Universal Links, and my target is macOS:

Code Block
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
.onOpenURL { url in
print("Inside onOpenURL....")
}
}
}

I have also configured the Associated Domains capability, and the apple-app-site-association file in my domain, so when opening the link to that domain, my app is opened instead of Safari.
So, when debugging the app, and clicking a Universal Link (for example, in Notes app), to open my app (which it does), I expect to see the "Inside onOpenURL...." message in the output window in Xcode, meaning the onOpenURL handler is handling the universal link.

But this does not happen. Every time I click the universal link, the app is opened but nothing is displayed in Xcode. This is in macOS Big Sur 11.1, and also 11.2 with a recent update.

However, if the same code is used to target an iOS 14 app, this works correctly, without changing any code, just the target of the app.

Trying many things, I also discovered that if I used a custom URL scheme (myapp:// for example), the onOpenURL handler is called, in my macOS app. I can see the "Inside onOpenURL...." message. But this does not happen with universal links, and my app is targeted to macOS and universal links are required.

Does anyone found this issue, and how to solve it?

Thanks

This question was posted a long time ago but, as I encountered the same problem on my universal app (i.e the universal link works perfectly on iPhone and iPad but the .onOpenURL was not called on macOS) and found a solution, I thought it could be useful to others.

Basically, for macOS, I use the .onContinueUserActivity modifier instead of .onOpenURL and everything works like a charm:

#if os(iOS)
      .onOpenURL { url in
        handle(universalLink: url)
      }
#elseif os(macOS)
      .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
        guard let url = userActivity.webpageURL else { return }
        // Do the same handling than in .onOpenURL
        handle(universalLink: url)
      }
#endif

One drawback is that you need some conditional building using preprocessor macros for iOS and macOS but it's something I can live with as this solution does not add too much noise to my code.

Hope it helps.

Accepted Answer

This question was posted a long time ago but, as I encountered the same problem on my universal app (i.e the universal link works perfectly on iPhone and iPad but the .onOpenURL was not called on macOS) and found a solution, I thought it could be useful to others.

Basically, for macOS, I use the .onContinueUserActivity modifier instead of .onOpenURL and everything works like a charm:

#if os(iOS)
      .onOpenURL { url in
        handle(universalLink: url)
      }
#elseif os(macOS)
      .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
        guard let url = userActivity.webpageURL else { return }
        // Do the same handling than in .onOpenURL
        handle(universalLink: url)
      }
#endif

One drawback is that you need some conditional building using preprocessor macros for iOS and macOS but it's something I can live with as this solution does not add too much noise to my code.

Hope it helps.

Spoke too soon

    var body: some Scene {
        WindowGroup {
                AppView(store: store)     
                    .onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
                 // handle it here
                    }
            }
        }

This will duplicate my current window. So now from one window I end up with 2 and 3 and so on.

SwiftUI apps in macOS are not triggering onOpenURL when receiving a Universal Link
 
 
Q