Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI macOS apps with App lifecycle are not triggering onOpenURL when receiving a Universal Link

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

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

like image 800
dacasta Avatar asked Sep 20 '25 20:09

dacasta


1 Answers

It looks like you have to use a different function for macOS sometimes.

.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
    guard let url = userActivity.webpageURL else { return }
    print("Inside onContinueUserActivity....")
}

I'd recommend keeping onOpenURL in case there are some circumstances where one works and the other doesn't (for example, onOpenURL will be triggered if you manually launch a deep link from within your own app).

like image 131
Colin Tremblay Avatar answered Sep 23 '25 12:09

Colin Tremblay