I want to open a specific ViewController
from the TabBarController
whenever a local notification is fired and their custom action is performed. I have used following line of code:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case "first":
DispatchQueue.main.async(execute: {
self.first()
})
case "second":
DispatchQueue.main.async(execute: {
self.second()
})
default:
break
}
completionHandler()
}
So it's a first()
function:
func first() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
let navigationController = storyboard.instantiateViewController(withIdentifier: "First") as! UINavigationController
tabBarController.present(navigationController, animated: true) {
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
Second function: second()
func second() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
let navigationController = storyboard.instantiateViewController(withIdentifier: "Second") as! UINavigationController
tabBarController.present(navigationController, animated: true) {
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
And it's works well, but I cannot open the second ViewController
while the first one is presented and the second notification is fired: In the console: Warning attempt to present ViewController...
Use this:
tabBarController.selectedIndex = 1
Or this:
tabBarController.selectedViewController = tabBarController.viewControllers![1]
Where 1 can be any of the viewcontrollers
presented by your tabBarController