Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
like image 279
Christopher Smit Avatar asked Jun 13 '17 18:06

Christopher Smit


People also ask

What happens with a deep link if the app is not installed?

If the app is installed, the user will be taken to the app via the deep link. If and only if the app is not installed, the click will not go anywhere. Therefore, bidders should only create deep links for users that they know have previously installed the app, based on data from their analytics SDK(s).

How do I know if my deep link is working?

By using Android Debug Bridge (ADB) shell commands one can test the deep link flow. It is used to verify if the link navigates to the correct section of your app. This command starts the ADB shell with the VIEW action and specifies the deep link URL to be tested.

How does app deep link work?

Deep links are a type of link that send users directly to an app instead of a website or a store. They are used to send users straight to specific in-app locations, saving users the time and energy locating a particular page themselves – significantly improving the user experience.

What is the difference between deep linking and deferred deep linking?

In the context of mobile apps, deep linking consists of using a uniform resource identifier (URI) that links to a specific location within a mobile app rather than simply launching the app. Deferred deep linking allows users to deep link to content even if the app is not already installed.


2 Answers

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
}
like image 165
Sviatoslav Yakymiv Avatar answered Sep 21 '22 16:09

Sviatoslav Yakymiv


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)
            }
        }
    }
like image 26
AndrewSas Avatar answered Sep 23 '22 16:09

AndrewSas