onemillion
onemillion

Reputation: 712

3D touch shortcut to a tab view and then performing a segue

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. 
}

Upvotes: 0

Views: 781

Answers (3)

Sean
Sean

Reputation: 67

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)
    }

Upvotes: 0

onemillion
onemillion

Reputation: 712

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.

Upvotes: 1

pkatsourakis
pkatsourakis

Reputation: 1082

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)

Upvotes: 0

Related Questions