0

I already created some (static) QuickActions in Info.plist for my iOS app created in Xcode and written in Swift.

I have problems with making them able to open a ViewController. Of cause, I already googled, but nothing worked for me. If this counts: I'm using ViewController managed by a TabBarController. Most tutorials seem to use NavigationController. But, I think it will be done with the segues, right? What code do I need to handle it?

Can anybody provide it please? Or does anybody know a simple manual/tutorial?

Regards, David.

P.S.: I tried this code, but it seems to work only with NavigationController?! Code:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void)
{
    self.handleShortcutItem(shortcutItem)
    completionHandler(true)
}

func handleShortcutItem(shortcutItem: UIApplicationShortcutItem)
{
    switch shortcutItem.type {
    case "icons.quickaction.home":
        self.presentComposeViewController()
    default: break
    }
}

func presentComposeViewController()
{
    guard let navigationController = window?.rootViewController as? UINavigationController else { return }

    let identifier = "MyViewController"
    let composeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier(identifier)

    navigationController.pushViewController(composeViewController, animated: false)
}
  • Not quite sure what you mean…`UITabBarController` has a `selectedIndex` property which shows one of its views. – saagarjha Jun 06 '16 at 20:01
  • And what code do I need to handle my QuickActions? –  Jun 06 '16 at 20:05
  • Is `composeViewController` part of the `UITabBarController`? – saagarjha Jun 06 '16 at 20:06
  • No, it isn't, I guess. I took this code from a sample project from GitHub and tried to get it work. Can you help me? What's my fault? –  Jun 06 '16 at 20:08
  • The view controller should probably be managed by something. You should have a navigation controller or a tab bar controller at the root. BTW, if you have the Github project I could look at that. – saagarjha Jun 06 '16 at 21:08
  • It isn't really GitHub, but…: I took project 60 from here: https://www.dropbox.com/sh/asvszzv8lnje2f0/AABWtzKFRb1tRxRzlew9FWiia?dl=0 The ViewControllers are managed by NavigationController in this project. But unfortunately I cannot/don't want to use NavigationController. My ViewControllers are managed by a TabBarController. Can you help me in some way? –  Jun 06 '16 at 21:26
  • I still can't picture your layout…could you screenshot your storyboard or something? – saagarjha Jun 06 '16 at 21:30
  • Actually I'm not in front of the computer, so I cannot provide a screenshot. But, I'm using four UIViewControllers managed by a single UITabBarController. –  Jun 06 '16 at 21:33
  • So is `composeViewController` part of the tab bar controller? If so, you can just set the `selectedIndex`. If it's a subview, then set the correct index and then push it. – saagarjha Jun 06 '16 at 21:43
  • I don't even know what composeViewController is. I just copied it from the project from the internet. –  Jun 06 '16 at 21:46
  • I can provide more and more specific informations tomorrow. –  Jun 06 '16 at 21:47
  • Hey @ILikeTau, thank you for trying to help me. I uploaded my project here: http://www.filedropper.com/myapp Can you please download and review it? –  Jun 07 '16 at 06:07
  • Your `composeViewController` is the third view controller in the `UITabBarController`. `guard let tabBarController = window?.rootViewController as? UITabBarController else { return }; tabBarController.selectedIndex = 2` should work (semicolon to break line). Don't forget to check for shortcuts while the app is launching. – saagarjha Jun 08 '16 at 02:59
  • Hey @ILikeTau thank you very much for your advice. That made it for me. You helped me very much. I'll post my working code as a new answer. –  Jun 08 '16 at 18:14
  • @ILikeTau, what code will I need for the first icon on the leftmost side? Just set the digit to 0? Because that doesn't work proper. –  Jun 08 '16 at 21:34
  • Okay, thank you. My other problem has nothing to do with your solution. –  Jun 09 '16 at 21:56

1 Answers1

0

I finally found the solution with the help from @ILikeTau.

I'm using the following code to open my ViewControllers managed by a TabBarController with QuickAction:

@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {

    if(shortcutItem.type == "app.quickaction.search"){
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        window?.rootViewController = vc
        guard let tabBarController = window?.rootViewController as? UITabBarController else { return };
                tabBarController.selectedIndex = 2
    }
    else if(shortcutItem.type == "app.quickaction.home"){
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        window?.rootViewController = vc
        guard let tabBarController = window?.rootViewController as? UITabBarController else { return };
        tabBarController.selectedIndex = 0
    }
}

This code works from both modes: app is in background mode and app is closed. I think this way is easier and shorter than the common way with multiple functions.

Community
  • 1
  • 1