Jaqueline
Jaqueline

Reputation: 485

Shortcut Items, Quick Actions 3D Touch swift

I am for some reason having some troubles figuring out the Shortcut Items in my app. I have followed all the tutorials and I believe that I have done everything right; however, it is acting really weird. The problem is that when you completely close out that app and do the 3D Touch Shortcut item on the home screen, it takes you to the correct part of the app as it should; however, when the app is open in the background (like you just click the home button) it doesn't take you to the correct part of the app, it just opens it. This is really odd and I am not sure what to do because I have followed all the instructions. Thank you so much!

Code:

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

    if shortcutItem.type == "com.myapp.newMessage" {

        let sb = UIStoryboard(name: "Main", bundle: nil)


        let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
         vc.selectedIndex = 2 //this takes me to the contacts controller



        let root = UIApplication.shared.keyWindow?.rootViewController

        root?.present(vc, animated: false, completion: {
            completionHandler(true)
        })


    } else if shortcutItem.type == "com.myapp.groups" {
        let sb = UIStoryboard(name: "Main", bundle: nil)


        let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
        vc.selectedIndex = 1 //this takes me to the groups controller

        let root = UIApplication.shared.keyWindow?.rootViewController

        root?.present(vc, animated: false, completion: {
            completionHandler(true)
        })

    } else {
        //more short cut items I will add later
    }
}

NOTE: these are static Shortcut Items, and I configured them in the info.plist file, This function that was presented is the only place in the app besides the info.plist file that have anything about the Shortcut Items.

EDIT: This seems like an issue with the RootViewController, because I have tried multiple different ways of doing this, and it still gives me the same warning message

2017-11-22 22:36:46.473195-0800 QuickChat2.0[3055:1068642] Warning: Attempt to present on whose view is not in the window hierarchy!

I just tried to accomplish my goals this way through this post but it gives me the exact same results as before.

Upvotes: 1

Views: 1207

Answers (2)

Ivan
Ivan

Reputation: 89

I think a cleaner method is to set your flags in the performActionFor method, then check for them once your VC loads. Using this notification will handle the shortcut every time the app becomes active, including launch.

override func viewDidLoad() {
    super.viewDidLoad()
    // Sends notification if active app is reentered to handle quick actions
    NotificationCenter.default.addObserver(self, selector: #selector(handleQuickActions), name: UIApplication.didBecomeActiveNotification, object: nil)

Upvotes: 0

Malik
Malik

Reputation: 3802

You should set a variable in your performActionForShortcutItem that tells you the shortcut type and move the code you have in there into applicationDidBecomeActive. Your could should look like this

var shortcut: String?

func applicationDidBecomeActive(application: UIApplication) {
    if let shortcutItem = shortcut {
        shortcut = nil
        if shortcutItem == "com.myapp.newMessage" {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
            vc.selectedIndex = 2 //this takes me to the contacts controller
            let root = UIApplication.shared.keyWindow?.rootViewController
            root?.present(vc, animated: false, completion: nil)
        } else if shortcutItem == "com.myapp.groups" {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
            vc.selectedIndex = 1 //this takes me to the groups controller
            let root = UIApplication.shared.keyWindow?.rootViewController
            root?.present(vc, animated: false, completion: nil)
        } else {
            //more short cut items I will add later
        }
    }
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    shortcut = shortcutItem.type
    completionHandler(true)
}

Upvotes: 1

Related Questions