Deep Linking only working if app is running
Asked Answered
N

5

10

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
    }
Naomanaomi answered 13/6, 2017 at 18:46 Comment(2)
Have you added url scheme to .plist file?Apportion
Yes. Like I said, it is working perfectly if the app is running on the phone. The only problem is, when I exit the app entirely(not minimize it), it will not work. The app have to be running for it to work. It won't execute if the app is not running. It only launches the app, but does not execute the code to navigate to the articleNaomanaomi
A
16

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
}
Alcaide answered 14/6, 2017 at 10:12 Comment(3)
PERFECT!! Well deserved accepted answer! Working as expected!Naomanaomi
got this error Error Domain=NSURLErrorDomain Code=-1101 "file is directory" UserInfo={NSLocalizedDescription=file is directory if URL = appname://path (deeplink path)Kazantzakis
Thanks! Still needed to implement AppDelagate nowadays because of Apple still supports devices such as iPhone 5s, iPhone 6 with max. iOS v12.x which of course does not support SceneDelegate but only this obsolete AppDelegateHallel
B
2

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)
}
Barger answered 12/5, 2021 at 11:31 Comment(0)
K
1

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)
            }
        }
    }
Karlkarla answered 22/8, 2018 at 12:3 Comment(1)
This! Thank you, only this has worked for me.Fendley
E
0
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 {}
Esteresterase answered 18/7, 2019 at 9:8 Comment(0)
I
0

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
        }
    }
}
Iconology answered 28/6, 2022 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.