简体   繁体   中英

Do something with Quick Action

Sorry if this is a dumb question, Setting up a 3D Touch quick action. I put everything into the.plist file and I had to put this into my AppDelegate.swift file

AppDelegate.swift

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.

    // Grab a reference to the shortcutItem to use in the scene
    if let shortcutItem = options.shortcutItem {
        shortcutItemToProcess = shortcutItem
    }

    // Previously this method only contained the line below, where the scene is configured
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) }

and this into my SceneDelegate.swift

// Shortcut code
    func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        // When the user opens the app through a quick action, this is now the method that will be called
        (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess = shortcutItem

    }
    // Shortcut code
    func sceneDidBecomeActive(_ scene: UIScene) {
        // Is there a shortcut item that has not yet been processed?
        if let shortcutItem = (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess {


            if shortcutItem.type == "com.application.Start" {
                print("Start Shortcut pressed")

                //let vc = ViewController ()
                //vc.startAct()

            }
            // Reset the shorcut item so it's never processed twice.
            (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess = nil
        }
    }

I want to run the function startAct() {... } that's in the Main app file called ViewController.swift

I tried

let vc = ViewController ()
vc.startAct()

and it sort of starts to run but crashed straight away on the first line with an error about unwrapping a nil value or something. Im guessing its not actually loading the Main view but trying to run it from the SceneDelegate.swift which cannot be correct.

Thank you in advance.

This seems to be causing some confusion so I'll show what I do.

You have to implement your response to the shortcut item in two places: in the scene delegate's willConnectTo and in performActionFor . To help you test, I'll just delete all my error checking and parsing and just demonstrate that we are in fact responding to the shortcut item:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    if let shortcutItem = connectionOptions.shortcutItem {
        let alert = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default))
        self.window?.makeKeyAndVisible()
        self.window?.rootViewController?.present(alert, animated: true)
    }
}

func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    let alert = UIAlertController(title: "Hello", message: "", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    self.window?.rootViewController?.present(alert, animated: true)
    completionHandler(true)
}

That shows the alert whether the app is suspended or terminated beforehand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM