Configuring Firebase Dynamic Links on iOS has several steps that must be followed. So, I recommend referring to the official documentation here. Additionally, there is a Firecasts series that walks through all of the steps here.
I followed the Firecasts series, but my setup is slightly different; so, I ran into a couple of issues. First, I am using a custom domain. Second, I'm using SwiftUI. I'll just address the issues that I had, so please refer to the full Firecasts series for all of the gruesome details.
SwiftUI 2.0
There are two different types of links that your app needs to handle. First, your app needs to handle links when your app is already installed (i.e. https://app.yourcustomdomain.com/...). Second, your app needs to handle links copied into the clipboard before your app was installed (i.e. com.your.bundle.id://google/link/...).
Also, you probably want to handle links originating from a web browser (Safari). In my case, I process a link from a QR code scan. Apparently NFC tag scan links also originate from Safari, but I can't confirm this.
After some trial and error, I found a way to handle all of these concerns with SwiftUI; it's unclear if this is the officially recommended approach, so proceed with caution.
First, use onOpenURL(perform:) and .onContinueUserActivity(_,perform:):
var body: some Scene {
WindowGroup {
ContentView()
.onContinueUserActivity(NSUserActivityTypeBrowsingWeb, perform: onWebBrowserActivity)
.onOpenURL(perform: onOpenURL)
}
}
Second, define the onContinueUserActivity handler:
// Handles links originating from the web browser
func onWebBrowserActivity(_ userActivity: NSUserActivity) {
guard let url = userActivity.webpageURL else {
return
}
onOpenURL(url)
}
Third, define the onOpenURL handler:
func onOpenURL(_ url: URL) {
let dynamicLinks = DynamicLinks.dynamicLinks()
if dynamicLinks.shouldHandleDynamicLink(fromCustomSchemeURL: url),
let dynamicLink = dynamicLinks.dynamicLink(fromCustomSchemeURL: url) {
handle(dynamicLink: dynamicLink)
} else {
handleUniversalLink(url: url)
}
}
Both types of links mentioned above are passed into the onOpenURL handler, so this if statement handles the two cases.
Finally, (as others have mentioned) handle universal links:
func handleUniversalLink(url: URL) {
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(url) { dynamicLink, error in
if let error = error {
print("Could not follow the dynamic link \(error.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
self.handle(dynamicLink: dynamicLink)
}
}
if linkHandled {
print("Link handled")
} else {
print("Link not handled")
}
}
Custom Domain
I had to follow the custom domain setup instructions here. Specifically, I had to set up Firebase Hosting, which requires adding an A DNS record (In addition to setting up a TXT DNS record for the Dynamic Links URL Prefix setup). Also, I had to specify the custom domain in my Info.plist
<key>FirebaseDynamicLinksCustomDomains</key>
<array>
<string>https://app.yourcustomdomain.com</string>
</array>