Search code examples
iphoneswift3xcode83dtouch

3D Touch actions not working (swift 3)


So, I have managed to get everything hooked up properly. When the short cut actions are pressed, I get the correct Print out in the console. Where do I go from here? I can't figure it out...

This is my code in App Delegate

    var quicklaunch: Bool!
    var viewName: String?

   func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    viewName = shortcutItem.type
    quicklaunch = true
    completionHandler(quicklaunch)
}

func applicationDidBecomeActive(application: UIApplication) {
    if quicklaunch == true {
        quicklaunch = false
        if viewName == "Messages" {
            let rootViewController = self.window?.rootViewController
            let popUpController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FriendsVC")
            rootViewController?.present(popUpController, animated: true, completion: nil)
            print("Worked")
        }
    }  
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    if quicklaunch == false {
        quicklaunch = true
        if viewName == "Messages" {
            let rootViewController = self.window?.rootViewController
            let popUpController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FriendsVC")
            rootViewController?.present(popUpController, animated: true, completion: nil)
            print("Worked")
        }
    }

}

How do I get it to pop up to the correct view controllers?


Solution

  • You can get the rootViewController and then present the required viewController modally (or push it onto navigation stack if your rootViewController is a UINavigationController). Something along the lines of

    let rootViewController = self.window?.rootViewController
    let popUpController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "???") as! ???
    rootViewController.present(popUpController, animated: true, completion: nil)
    

    Replace ??? with corresponding values

    EDIT

    Make a boolean variable in your AppDelegate called quickLaunch and a string variable viewName. Then update the following two methods

    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        viewName = shortcutItem.type
        quickLaunch = true
        completionHandler(quickLaunch)
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        if quickLaunch == true {
            quickLaunch = false
            //PRESENT YOUR VIEW MODALLY HERE
        }  
    }