Deep Linking only works if the app is running

I have an application that uses deep linking to navigate to a page when a user shares certain content in the application with another user. This works when the second user has the app already running, but if the app doesn't work, it just opens the app and stays on the home screen. I know I must be missing something really simple, but I just can't seem to figure it out and can't find answers to 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
    }

      

+5


source to share


3 answers


This is the correct behavior. You have to process it inappliction(_: 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
}

      

+9


source


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)
            }
        }
    }

      

0


source


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 {}

      

0


source







All Articles