rice888
rice888

Reputation: 51

How to call a view controller via Quick Actions (3D touch) Swift

I'm trying to implement a 3D touch command where if the user presses "New scan", then the view controller ProcessedImageViewController is called. I have already set up the Info.plist to create the quick option, but I am having trouble actually calling the ProcessedImageViewController when "New scan" is pressed. Here is my code from the AppDelegate.swift:

var launchedShortcutItem: UIApplicationShortcutItem?
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        
        
        
        if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem{
            launchedShortcutItem = shortcutItem
        }
      
        return true
    }
    
    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        completionHandler(handleShortcutItem(item: shortcutItem))
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        
        guard let shortcutItem = launchedShortcutItem else { return }
        //If there is any shortcutItem,that will be handled upon the app becomes active
        _ = handleShortcutItem(item: shortcutItem)
        //We make it nil after perfom/handle method call for that shortcutItem action
        launchedShortcutItem = nil
}
func handleShortcutItem(item: UIApplicationShortcutItem) -> Bool {
        var handled = false
        // Verify that the provided shortcutItem's type is one handled by the application.
        let mainStoryboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        var reqVC: UIViewController!
        reqVC = mainStoryboard.instantiateViewController(withIdentifier: "ProcessedImageViewController") as! ProcessedImageViewController
        handled = true
        if let homeVC = self.window?.rootViewController as? UINavigationController {
            homeVC.pushViewController(reqVC, animated: true)
        } else {
            return false
        }
        
        return handled
    }

When I try to click on "New scan" in the Quick Actions menu, I only get taken to the Root View controller (not ProcessedImageViewController).

Upvotes: 1

Views: 75

Answers (0)

Related Questions