Linking 3D Touch Static Shortcuts To Tabbed/Navigation View Controllers In Swift 4

In this example, I have a Swift 4 app with a single Tab Bar Controller and three Navigation Controllers each segued to a View Controller. There are plenty of guides online to help you set up the necessary strings in your Info.plist to enable static 3D touch shortcuts. In my case, I have three shortcuts – each is linked to a different view controller.

Place the following function into your AppDelegate.swift file.

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)
    {
        guard let tabBarController = window?.rootViewController as? UITabBarController else 
        {
            return
        }
        switch shortcutItem.type 
        {
        case "UIApplicationShortcutItemType-0":
            tabBarController.selectedIndex = 0
        case "UIApplicationShortcutItemType-1":
            tabBarController.selectedIndex = 1
        case "UIApplicationShortcutItemType-2":
            tabBarController.selectedIndex = 2
        default:break
        }
    }

The bold code should match the UIApplicationShortcutItemType you placed in Info.plist for each shortcut. 🙂

You might also like

Leave A Reply

Your email address will not be published.