0

I am having an issue using the 3D touch shortcuts inside my application. My app uses tabs but I would like to redirect the user into a tab then also into another segue when they press the create wish list button.

Here is a picture of my storyboard.

The code I am using at the moment displays the home view controller but I would like it to go into the create wish list controller.

The code I have for the handle shortcut inside the app delegate is here:

   func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
            print("Handling shortcut")

            var succeeded = false

            if( shortcutItem.type == "com.example.Giftr" ) {

                print("- Handling \(shortcutItem.type)")

                if let tabVC = self.window?.rootViewController as? UITabBarController{
                tabVC.selectedIndex = 1 
                //This is where I need to swap to the "createwishlist" view controller. 
}
onemillion
  • 682
  • 5
  • 19
  • So this code successfully goes to the right tab, then your issue is doing a segue after that? – pkatsourakis Feb 09 '16 at 01:16
  • Yeah that's right it goes into the correct tab but when I present the view controller the tabs then go away and also the navigation controls at the top. – onemillion Feb 09 '16 at 02:14
  • Did you ever get this figured out? I'm currently having the same problem – Kyle H Mar 10 '16 at 05:39

3 Answers3

1

To solve this I used a Global Variable to store that the shortcut had been taken inside the appDelegate like below.

          GlobalVars.shortcut = 1
            let tabVC = window?.rootViewController as! UITabBarController
            print(tabVC.self)
            tabVC.selectedIndex = 0

Then inside the controller of the tab for the selectedIndex 0 checked if that value was 1 and if it was then segued to the view controller that I wanted to end up at. Like shown.

override func viewDidAppear(animated: Bool) {    
        if(GlobalVars.shortcut == 1)
        {
            self.performSegueWithIdentifier("shortcut", sender: self)
            GlobalVars.shortcut = 0
        }
    }

Make sure that you set the Global Variable to 0 or this will be called every time the view appears.

onemillion
  • 682
  • 5
  • 19
0

This should be able to change the ViewController to your desired class after you successfully switch tabs.

let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)
pkatsourakis
  • 1,024
  • 7
  • 20
  • That works but I would like it to have the tab back into the home view controller so I would like to "perform a segue" as such. It is hard to explain but right now it loads the view with no navigation or tabs but I would like to load it with the navigation bar at the top and also the tab selected. – onemillion Feb 09 '16 at 02:13
0

I would use NotificationCenter. Let's say your app naturally launches to HomeViewController, which happens to be the first VC of the main tab bar controller, and that you want to define "New Post" shortcut item so that as soon as HomeViewController is loaded it performs segue to NewPostViewController.

First, define notification name:

extension Notification.Name {

    Notification.Name("applicationLaunchedWithNewPostShortcutItem")

}

Second, post notification when the app is launched with the shortcut item (I'm assuming it's the first static shortcut item):

func handleShortCutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool {
    var handled = false

    guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false } 
    guard let shortCutType = shortcutItem.type as String? else { return false }

    switch shortCutType {
    case ShortcutIdentifier.first.type:
        NotificationCenter.default.post(name: .applicationLaunchedWithNewPostShortcutItem, object: nil, userInfo: nil)
        handled = true
        break
    default:
        break
    }

    return handled
}

For those who are not familiar with above method, it is usually defined in your AppDelegate.swift and called in application(_:performActionFor:completionHandler:), also in AppDelegate.swift. See Apple's sample code here.

Lastly, allow HomeViewController to be able to tune-in to the notification by implemening addObservers() and removeObservers() method in it.

class HomeViewController {

    // MARK: - View Controller Life Cycle

    override func viewDidLoad() { // or viewWillAppear(), etc.
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: .applicationLaunchedWithNewPostShortcutItem, object: nil, queue: .main) { [weak self] (notification) in
            self?.handleApplicationLaunchedWithNewPostShortcutItem(notification: notification)
        }
    }

    deinit { // or viewWillDisappear(), etc.
        removeObservers()
    }

    // MARK: - Notification Center Handlers

    private func handleApplicationLaunchedWithNewPostShortcutItem(notification: Notification) {
        performSegue(withIdentifier: "presentNewPost", sender: nil)
    }

Sean
  • 59
  • 2