I am trying to open specific view controller on button click in today extension, but can not show it. The main app is opening correctly on the main root controller using url scheme but not the specific view controller I set up in App Delegate.
I tried ALL tutorials and answers I could find on Google, for examble: How to open Specific View controller on Widgets/ Today Extension click
but I can't get it to work. I tried to figure out where the problem is and I think the method in app delegate do not call. I wrote some print() Methods but none of them appeared in console. I have configured the URL Scheme correctly like in the answers from the link above, so I don't know why the method in App Delegate don't execute.
Hope someone has a tip what else I can try.
Code from Button IBAction in TodayViewController:
@IBAction func addButtonOnePressed(_ sender: UIButton) {
let url = URL(string: "OpenURL://")!
self.extensionContext?.open(url, completionHandler: nil)
}
Code from App Delegate Method (I tried all I could find of them):
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
{
if url.scheme == "OpenURL"
{
let storyboard = UIStoryboard(name: "Groups", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Group") as! MasterViewController
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
return true
}
UPDATE: I solved it myself. I have to do all the stuff in SceneDelegate and not in AppDelegate and take care of the Tabbar Controller and the navigation Controller.
New Code in SceneDelegate.swift:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
if let url = URLContexts.first?.url {
if url.scheme == "OpenURL" {
guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
return
}
let storyboard = UIStoryboard(name: "Groups", bundle: nil)
if let rootVC = storyboard.instantiateViewController(withIdentifier: "Group") as? MasterViewController,
let tabBarController = rootViewController as? UITabBarController,
let navController = tabBarController.selectedViewController as? UINavigationController {
navController.pushViewController(rootVC, animated: true)
}
}
}
}