Documentation Issue: Deeplinks not working when the app is closed (Scene based apps)
mauronate opened this issue · 3 comments
Hello,
I am writing this because I think the Branch documentation is missing a very important step to make deeplinks (and not universal links) work when the app is closed.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
// workaround for SceneDelegate continueUserActivity not getting called on cold start
if let userActivity = connectionOptions.userActivities.first {
BranchScene.shared().scene(scene, continue: userActivity)
}
}
Essentially this code is only handling when the scene is created while the app is launching the universal links, which are seen by iOS as a userActivities, however deeplinks are seen in scene based apps as urlContexts, which can be fetched from the options informations on connection of the scene (they should only be fetched when not empty)
The recommended way to handle both universal links and deeplinks at app launch would be the following then:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
// workaround for SceneDelegate continueUserActivity not getting called on cold start
if let userActivity = options.userActivities.first {
// Universal Links
BranchScene.shared().scene(scene, continue: userActivity)
} else if !options.urlContexts.isEmpty {
// Deeplinks
BranchScene.shared().scene(scene, openURLContexts: URLContexts)
}
}
I hope this may fix any issue with deeplinks not working when the app is closed, and improve further the Branch's doc.
Do you have any steps to reproduce this issue? Because I was unable to find any issue with deep links not working when the app is closed.
The steps would be:
- Open the app
- Kill the app by bringing the app manager on iOS (swipe from the bottom)
- Try to open a deeplink (must be opened from safari, the deeplink usually starts with the app scheme, and not https:// example: app://)
Thank you! Previously, I was opening the deep links from the Reminders and Messages apps, which work as intended. But after trying Safari, I experienced the app opening, but no deep link params being passed through.
I've implemented your fix and everything is working smoothly now. Thanks again for bringing this to our attention.