Firebase Dynamic Links with Scene Delegate
In general, Firebase by Google handles deep and Dynamic Links very well for iOS. But iOS 13 comes with many changes (Scene Delegate!) that aren’t yet fully explained. This is why I made this tutorial. What’s the fuss about deep links in iOS 13? Let's check it out!
What are Firebase Dynamic Links?
If you’re reading this post, you probably know what Dynamic Links are. To make double sure that it is so, deep links and Dynamic Links are urls that link to your app.
- They can:
- open an app,
- pass some data to it,
- check if a user has the particular app installed or not,
- or open AppStore (or Google Play for Android) with the download path.
Learn more on Firebase Dynamic Links from the video below:
My use case for Dynamic Links in iOS 13
I needed to use all the options shown above. First - I wanted the link to "know" if a user has the app installed on their iPhone. If yes - open it and pass some data. If no - it should have opened AppStore with the download path. Simple?
Well, with Firebase it is kind of simple. However, it is the case only for older iOS versions. Problems started when I passed the tutorial and added all needed lists and functions. Dynamic links started to work well, but with one problem - after opening the app, no data showed up. Soon I found out that the problem was with iOS 13 and Scene Delegate.
App Delegate and Scene Delegate
In older iOS versions, all you needed was to implement 3 functions in App Delegate. But if your project is built with Xcode 11 and targets iOS 13 - which means it comes with Scene Delegate - there are a few steps more.
Besides adding functions mentioned in Google’s tutorial, you need to go to Scene Delegate and add:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
and:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity)
In both functions, we need to implement handler for receiving dynamic links. I recommend creating a separate class for that. This will help us organize the whole structure. In iOS 13 and its newer iterations we’ll be receiving Dynamic Links via Scene Delegate while on older devices this will be done through AppDelegate. It can be a little messy if we don’t take care of proper events handling.
This is not the end yet. After implementing mentioned functions in Scene Delegate, our app should be ready for receiving Dynamic Links from Firebase. But not when it is closed. We also need to take care of this state of the app. Fortunately, this can be easily solved thanks to this generic function:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
From connectionOptions
we need to get the activity.webpageURL
object and pass it to:
DynamicLinks.dynamicLinks().handleUniversalLink(url)
This will work when the app is shut down, but we still want to pass the data through a Dynamic Link.
I hope this little tutorial will help you with Firebase Dynamic Links in iOS 13. Good luck!