Reputation: 99
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
Upvotes: 4
Views: 1325
Reputation: 11
Had the exact same problem and Colin's answer didn't work for me for some reason! I had to create an App Delegate, make it an ObservableObject
, put a published property to observe later, and implement the "open" method:
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
@Published var urls = [URL]()
func application(_ application: NSApplication, open urls: [URL]) {
self.urls = urls
}
}
Then of course add the adaptor to the @main
file:
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
Then in the ContentView()
or wherever add it as an @EnvironmentObject
:
@EnvironmentObject private var appDelegate: AppDelegate
...and only then could I use it, something like:
.onChange(of: appDelegate.urls) { urls in
dump(urls)
}
No idea if it's a good solution or not, but it's the only thing that worked for me. Got the idea from here: https://developer.apple.com/documentation/swiftui/nsapplicationdelegateadaptor and then here: https://developer.apple.com/documentation/appkit/nsapplicationdelegate
Upvotes: 1
Reputation: 387
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).
Upvotes: 4