3D Touch快速操作完全不起作用

5
我浏览了所有其他的问题,但我无法弄清楚出了什么问题。我从Apple Developer网站下载了示例项目添加主屏幕快速操作,那个没有问题,但是当我启动一个新的Xcode项目并复制它时,它对我不起作用。我一定漏掉了什么东西。现在,我只想让它在控制台中打印“按下快捷方式”。当我在我下载的Apple项目中添加print(“Shortcut pressed”)时,它可以正常工作。
目前我只是随意尝试各种东西。
我已经使用数组、字典和字符串更新了我的info.plist,并复制和粘贴了值,以免出现任何拼写错误。
UIApplicationShortcutItems,Item 0,UIApplicationShortcutItemType,UIApplicationShortcutItemIconType,UIApplicationShortcutItemTitle 按下应用程序图标时,这个快捷方式出现了,但它只是打开了应用程序。
这是我的非常基本的AppDelegate.Swift文件,只是试图让它做任何事情。可能是我的项目设置有问题,我的Xcode版本是最新的 - 版本11.1(11A1027)
我以前从未使用过快捷操作,它似乎很简单,只需在plist中添加几行代码,并在AppDelegate.Swift文件中添加一些代码,但是要使其正常工作需要很长时间。
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

     var shortcutItemToProcess: UIApplicationShortcutItem?

    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 {
            shortcutItemToProcess = shortcutItem
            print("Shortcut pressed")
        }
        return true
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        print("Shortcut pressed")
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        print("Shortcut pressed")
    }

    // MARK: UISceneSession Lifecycle

    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.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

谢谢。

1个回答

22

看起来你的应用是基于场景的。对于基于场景的应用程序,你几乎可以忘记 AppDelegate ,并专注于 SceneDelegate 。现在,你需要重写 SceneDelegate 中的两个方法和 AppDelegate 中的一个方法。为了清晰明了,我将模拟苹果的指南:

如果用户正在打开应用程序,并且这是一次全新启动,那么在 AppDelegate 中处理此操作:

     // 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)
     }

如果用户在单击快捷方式项目时您的应用仍在后台运行,您需要在SceneDelegate中处理:

    // SceneDelegate.swift
    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
    }

场景准备就绪后,您可以使用快捷方式进行所需操作:

    // SceneDelegate.swift
    func sceneDidBecomeActive(_ scene: UIScene) {
        // Is there a shortcut item that has not yet been processed?
        if let shortcutItem = (UIApplication.shared.delegate as! AppDelegate).shortcutItemToProcess {
            // In this sample an alert is being shown to indicate that the action has been triggered,
            // but in real code the functionality for the quick action would be triggered.
            var message = "\(shortcutItem.type) triggered"
            if let name = shortcutItem.userInfo?["Name"] {
                message += " for \(name)"
            }
            let alertController = UIAlertController(title: "Quick Action", message: message, preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
            window?.rootViewController?.present(alertController, animated: true, completion: nil)

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

谢谢,伙计。我会仔细看你的答案。我之前也遇到过通知操作的类似问题,我在AppDelegate文件中所做的一切都没有结果,但当我把所有东西放到ViewController.swift文件中时,它开始工作了。我认为这可能也是这个问题的情况,感谢你的回复,明天我会告诉你是否有帮助。 - Kurt L.
谢谢你的帮助。现在我的活动控制台上显示“快捷键已按下”。太棒了!再次感谢,我现在可以开始让它做事情了。祝你有美好的一天。- Kurt。 - Kurt L.
嗨@BillyBrawner,你可能早就看到了这一点,但我在做一些研究时会问一下,应用程序打开并且我在ViewController.swift文件中有一个函数,当他们按下快捷方式时,我想要启动它,它被称为startAct()。您是否可以友善地指导我如何从SceneDelegate.swift开始按下快速操作时的方向,因为显然目前它说Use of unresolved identifier 'startAct' - Kurt L.
谢谢回复。我尝试将这个代码放到SceneDelegate中,在它打印快捷键按下的地方下面:vc = ViewController() vc.startAct(),但是当它尝试运行startAct()函数时,它崩溃了,并在第一行的startAct()函数上显示了一个红线,提示尝试解包一个可选项。第一行是startButton.isHidden = true,我猜测它无法从SceneDelegate中找到start button。 - Kurt L.
2
要在“SceneDelegate”中访问该“shortcutItem”而不是应用程序委托,您也可以访问此委托func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)。这将转换为if let shortcutItem = connectionOptions.shortcutItem { shortcutItemToProcess = shortcutItem } - Daisy R.
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接