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.