I am trying to implement universal linking into my app using the AppDelegate
class. It is working perfectly fine when the app is already open in the background, but it is acting strange while the app is NOT in the background, specifically it will only work 1 time if the app is not in the background. For example: When I boot up my app for the first time, multi-task then swipe out of the app I can click a link and it will navigate to the correct place in the app. If I try that again it will not work, but If I reboot the app it will work once again. Keep in mind if the app is in the background the universal linking will work every time.
Here is what my AppDelegate
looks like:
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let url = userActivity.webpageURL {
self.continueWithLink(url: url)
}
return true
}
in didFinsihLaunchingWithOptions
function:
self.window = UIWindow(frame: UIScreen.main.bounds)
if let url = launchOptions?[UIApplication.LaunchOptionsKey.url] as? URL {
self.continueWithLink(url: url)
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "MainTabBarViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
The continueWithLink
function will decipher where the link is wanting to navigate to and then will call a function that sets the desired viewController like this:
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
if let _ = appDelegate.window?.rootViewController as? BannedViewController { return }
let storyboard = UIStoryboard(name: "Profile", bundle: nil)
let profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileView") as! ProfileViewController
profileVC.shouldNavigateToHome = true
profileVC.shouldNavigateToHomeAction = {
self.loadMainStoryboard()
}
let navigationVC = UINavigationController(rootViewController: profileVC)
appDelegate.window?.rootViewController = navigationVC
appDelegate.window?.makeKeyAndVisible()