2

I'm stting up quick actions for an app. It works fine. So in AppDelegate I am checking for the shortcutItem like this and call a completionHandler function:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    completionHandler(handleShortcut(shortcutItem: shortcutItem))
}

Now I'd like to open a certain tab in the handleShortcut function, but this doesn't work at all. I tried loading it as a view with it's storyboardID and add it as a rootViewController, but I am not even sure if this would work at all or if this is the right way to do it:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "tabBar")
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible()

But obviously I cannot use tabBarController?.selectedIndex = 2 for instance to access the third tab.

What would be the right way to access another tab in my tabBarController (and show it) through Quick Actions?

RjC
  • 827
  • 2
  • 14
  • 33

1 Answers1

0

I have come to a solution, but I am not sure if this is the best way to do it:

if shortcutItem.type == "com.xxx.openHelp" {
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let vc = storyBoard.instantiateViewController(withIdentifier: "tabBar")
            self.window = UIWindow(frame: UIScreen.main.bounds)
            self.window?.rootViewController = vc
            let myTabBar = self.window?.rootViewController as! UITabBarController
            myTabBar.selectedIndex = 3
            self.window?.makeKeyAndVisible()
        }
RjC
  • 827
  • 2
  • 14
  • 33