0

Does anyone have good idea to handle the deeplink? I want to push a single page view which needs id from the HomeViewcontroller(or anything is ok) to the single page with the id that I get from the deeplink.

My current situation is that I could get the deeplink and the id inside of that in AppDelegate file by the way like below.

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
    if let incomingURL = userActivity.webpageURL {
        let linkHandled = DynamicLinks.dynamicLinks()!.handleUniversalLink(incomingURL) { [weak self](dynamiclink, error) in
            guard let strongSelf = self else { return }
            if let dynamiclink = dynamiclink, let _ = dynamiclink.url {
                strongSelf.handleIncomingDynamicLink(dynamicLink: dynamiclink)
            }
        }
        return linkHandled
    }
    return false
}

func handleIncomingDynamicLink(dynamicLink: DynamicLink) {
    guard let pathComponents = dynamicLink.url?.pathComponents else {
        return
    }

    if pathComponents.count > 1 {

        for (i, value) in pathComponents.enumerated() {
            if i == 1 {
                // define whether the deeplink is for topic or post
                UserDefaults.standard.set(value, forKey: "deepLinkType")
                print(value)
            } else if i == 2 {
                UserDefaults.standard.set(value, forKey: "deepLinkId")
                print(value)
            }
        }
    }

}

And then viewDidAppear in the HomeViewController

        if (self.isViewLoaded && (self.view.window != nil)) {
        let us = UserDefaults.standard

        if let deepLinkType = us.string(forKey: "deepLinkType"), let deepLinkId = us.string(forKey: "deepLinkId"){

            us.removeObject(forKey: "deepLinkType")
            us.removeObject(forKey: "deepLinkId")

            if deepLinkType == "topic" {

                let storyboard = UIStoryboard(name: "Topic", bundle: nil)
                let nextVC = storyboard.instantiateViewController(withIdentifier: "SingleKukaiVC") as! TopicViewController
                nextVC.topicKey = deepLinkId

                self.navigationController?.pushViewController(nextVC, animated: true)

            } else if deepLinkType == "post" {

            }
        }
    }

this works fine when the app is neither in foreground nor background I mean if it's not instanced. However, while the app is instanced, this doesn't work because viewDidAppear is not going to be read. Or even the HomeViewController itself is not might be called if user had opened another view.

So my question is that what is the best way to handle the deeplink which has id for the single page? I appreciate some examples.

Thanks in advance.

2 Answers2

0

Two ways you could handle it:

  1. For the ViewController that you want presented upon handling of the deeplink set an initializer that takes info contained in the deeplink. And set whatever you need to set to handle that info(you seem to have done that). Now in your handleIncomingDynamicLink() instantiate the ViewController we mentioned and make it to be presented. How you are going to make it present itself depends on the navigation logic that you have set.

    AppDelegate receives link->Handles it->Instatiates VC and presents it->Does things accordingly

  2. In your handleIncomingDynamicLink() use NotificationCenter to post a notification containing the info. In your ViewController add an observer for that notification and define whatever you need to be done when the notification is received.

    AppDelegate receives link->Handles it->Fires notification->VC listens notifications->Does things accordingly

Community
  • 1
  • 1
Do2
  • 1,751
  • 1
  • 16
  • 28
0

Don't write your code to push Topic view controller in the HomeViewController.

Inside the App Delegate's handleIncomingDynamicLink method, get the top most view controller, then create the view controller (as in your code) and then push it from the top most view controller.

Your code to create Topic View Controller:
let storyboard = UIStoryboard(name: "Topic", bundle: nil)
                let nextVC = storyboard.instantiateViewController(withIdentifier: "SingleKukaiVC") as! TopicViewController
                nextVC.topicKey = deepLinkId

Check URL to see how to fetch top most view controller

Satyam
  • 15,493
  • 31
  • 131
  • 244