Do something with Quick Action

≡放荡痞女 提交于 2019-12-25 17:45:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/58479013/do-something-with-quick-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!