Christopher Smit
Christopher Smit

Reputation: 968

Deep Linking only working if app is running

I have an app that uses deep linking to navigate to a page when a user shares specific content in the app with another user. This is working when the second user has the app already running, but if the app is not running it simply opens the app and remains on the main screen. I know I must be missing something really simple here, but I just can't figure it out and can't find any answers regarding this on google.

My code in AppDelegate.swift:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let urlPath : String = url.path as String!
        let urlHost : String = url.host as String!
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        if(urlHost != "example.com")
        {
            print("Call Not From Correct Host. Not Continuing...")
            return false
        }

        if(urlPath == "/articles"){

            let article: ArticleDetailsViewController = mainStoryboard.instantiateViewController(withIdentifier: "ArticleDetailsViewController") as! ArticleDetailsViewController
            self.window?.rootViewController = article
        } 
        self.window?.makeKeyAndVisible()
        return true
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        return true
    }

Upvotes: 10

Views: 7404

Answers (5)

surfrider
surfrider

Reputation: 1425

For SceneDelegate:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    /* some stuff */
    for activity in connectionOptions.userActivities {
        if let url = activity.webpageURL {
            // handle
        }
    }
}

Upvotes: 0

ajith Kumark
ajith Kumark

Reputation: 393

If you are using sceneDelegate scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) function will work when app is launched after terminated state.

url for deeplinking will be available in connectionOptions.urlContexts

 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    navigateToDeepLinkScreen(urlContexts: connectionOptions.urlContexts)
}

Upvotes: 2

Antons Aleksandrovs
Antons Aleksandrovs

Reputation: 19

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    if let url = launchOptions?[.url] as? URL {
        return handleWidgetUrl(url)
    }

    return true
}
// Custom function to handle url and do some actions
private func handleWidgetUrl(_ url: URL) -> Bool {}

Upvotes: 0

AndrewSas
AndrewSas

Reputation: 843

For Swift4.2

if launchOptions != nil{
        let launch = launchOptions![UIApplicationLaunchOptionsKey.userActivityDictionary]! as! Dictionary <String,Any>

        if ((launch["UIApplicationLaunchOptionsUserActivityKey"]! as! NSUserActivity).webpageURL != nil){
            if defaults.bool(forKey: DEFAULTS_IS_FIRST_START){
                print((launch["UIApplicationLaunchOptionsUserActivityKey"]! as! NSUserActivity).webpageURL)
            }
        }
    }

Upvotes: 1

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

This is correct behavior. You should handle it in appliction(_: didFinishLaunchingWithOptions:)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    if let url = launchOptions[.url] as? URL, let annotation = launchOptions[.annotation] {
        return self.application(application, open: url, sourceApplication: launchOptions[.sourceApplication] as? String, annotation: annotation)
    }
    return true
}

Upvotes: 16

Related Questions