Universal Links Camera does not pass url to .onOpenURL (perform :) method SwiftUI Xcode

Hello, I connected Universal Links between the website and the application, everything was done according to the official documentation, and everything works fine, when the site is launched, safari offers to launch the application, there is also a banner above the launch page. In SwiftUI, I listen to the URL via .onOpenURL (perform :), everything works, but if the link is encoded in a QR code and scanned through the camera, a plate will appear prompting you to go to the application, and it goes, but only does not pass the URL to the .onOpenURL method (perform: ) . What could be the problem ?

Replies

The same thing happens when you scan an NFC tag containing the URL. At least me and someone else have this issue:

https://stackoverflow.com/questions/65150897/swiftui-universal-links-not-working-for-nfc

Their work-around is to use a custom URL scheme, but since that's frowned upon these days it would be nice if this was fixed. I assume it's an issue in SwiftUI (or maybe something lower level - I haven't written a UIKit test app to narrow down where the URL ges lost).

I did make a quick test to see if custom schemes work, but I could only get it to work when scanning a QR code - not when scanning an NFC tag.
So the solution is to add a continue user activity handler on a suitable view:
Code Block
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { userActivity in
print("Continue activity \(userActivity)")
guard let url = userActivity.webpageURL else {
return
}
print("User wants to open URL: \(url)")
// TODO same handling as done in onOpenURL()
}

It's not obvious why the same Universal Link received through a click on a link in Safari should be handled differently than the same link read from a tag, but apparently it's not the same thing.

  • Thank you so much! I wasted hours of debugging and with this simple fix it's working now. THANK YOU!

  • Thank you! This is sooo stupid on Apple's part. But thank you for the easy workaround!

  • Great answer!

Where were you two days ago?

Thank you!