1

I have been struggling with this for a few hours.

I have a UITabBarController, inside I have UINavigationController, and inside that I have a ViewController with a tableview in it.

All the tutorials have NavigationController as the parents, and everything is inside that.

Here's what my Main.storyboard looks like (I'm trying to make the shortcut go to the third ViewController on the top row, or the second ViewController on the second row down):

enter image description here


Here is part of my info.plist file:

    <key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeHome</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Home</string>
            <key>UIApplicationShortcutItemType</key>
            <string>com.emmetarries.Tabbed-Test.home</string>
        </dict>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeLatest</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>Latest Photo</string>
            <key>UIApplicationShortcutItemType</key>
            <string>com.emmetarries.Tabbed-Test.latestPhoto</string>
        </dict>
    </array>

I've looked into these already:

DuckDuckGoed: "3D quick actions with UITabBarController", no results besides those below.

Handle 3D Touch quick actions with UITabBarController and UINavigationController: Objective-C

iOS 9 selecting tabbar index with quick actions, performActionForShortcutItem: Objective-C

I've tried following all tutorials I can find in Stack Overflow and Youtube.


Does anyone know how this is done, or do you have a tutorial you found that explains this in Swift 3? I can re-order/arrange my views, I just haven't found a good way to do it.

Community
  • 1
  • 1
appmaster
  • 63
  • 6

1 Answers1

1

Have a try with this :

In your AppDelegate.swift :

enum QuickAction: String {
case Home = "home"
case LatestPhoto = "latestPhoto"

init?(fullIdentifier: String) {
    guard let shortcutIdentifier = fullIdentifier.components(separatedBy: ".").last else {
        return nil
    }

    self.init(rawValue: shortcutIdentifier)
 }
}

We will use this to determine which action you choose. Then we need to implement the function to handle the action :

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

private func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
    let shortcutType = shortcutItem.type
    guard let shortcutIdentifier = QuickAction(fullIdentifier: shortcutType)
        else {
            return false
    }

    guard let tabBarController = window?.rootViewController as? UITabBarController else {
      return false
    }

    switch shortcutIdentifier {
    case .Home:
        tabBarController.selectedIndex = 0
    case .LatestPhoto:
        tabBarController.selectedIndex = 1
    }
    return true
}

And it is done !

Antoine
  • 1,139
  • 9
  • 21
  • Thanks! I'll look at it in the morning! – appmaster Apr 09 '17 at 08:17
  • I have essentially what you put there. I really need the `latestPhoto` shortcut to go to the 3rd (farthest right) VC on the top row. I found something that does that, but it doesn't work when the app is started after quitting. It only works if the app is already in the background. To see this, please download the project: https://github.com/jgonfer/3d-touch-tutorial-swift/tree/tutorial_part_1, do you know why that is?? Thank You! – appmaster Apr 10 '17 at 06:27