Logan
Logan

Reputation: 1212

Universal Links from Closed App not working

I have my app set-up to handle Universal links. We handle the retrieval of the URL, and the navigation given the URL inside of this function:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        guard NSUserActivityTypeBrowsingWeb == userActivity.activityType, let url = userActivity.webpageURL else {
            return false
        }
        // Do navigation to correct page based on URL
        return true
    }

This works perfectly for us when our app was previous running in the background, and a universal link is tapped.

However, when I kill the app completely and click on a universal link, this function doesn't appear to be running and so it doesn't handle the universal link at all, it just brings the user to the home tab.

I've tried grabbing the URL in application(didFinishLaunchingWithOptions:) and application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) but neither of those seem to get called as well.

Does anyone have any ideas on this?

Thank you!

Upvotes: 13

Views: 3110

Answers (3)

David Jourdan Manalu
David Jourdan Manalu

Reputation: 21

You can check in

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

You can get the URL by checking connectionOptions.userActivities (it's a Set<NSUserActivity>).

Get the URL from webpageURL Here's the example:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    for userActivity in connectionOptions.userActivities where userActivity.webpageURL != nil {
        // call your universal link handler here
    }
}

I hope it can help you fix your problem.

Upvotes: 2

Dmih
Dmih

Reputation: 616

Swift 5 IOS 14 When app is terminated use

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Get URL components from the incoming user activity.
        guard let userActivity = connectionOptions.userActivities.first,
              userActivity.activityType == NSUserActivityTypeBrowsingWeb,
              let incomingURL = userActivity.webpageURL
        else { return }
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            LinkHandler.sharedInstance.handleLink(url: incomingURL)
        }
        guard let _ = (scene as? UIWindowScene) else { return }
    }

Upvotes: 14

Julius
Julius

Reputation: 173

On application(didFinishLaunchingWithOptions:) url is inside an options. Please test it again.

Apple docs: launchOptions A dictionary indicating the reason the app was launched (if any). The contents of this dictionary may be empty in situations where the user launched the app directly. For information about the possible keys in this dictionary and how to handle them, see Launch Options Keys.

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application

Upvotes: 1

Related Questions