ios - 3D Touch 快速操作根本不起作用

标签 ios swift xcode quickaction

我已经解决了所有其他问题,但我无法弄清楚出了什么问题。
我从 Apple Developer 站点 Add Home Screen Quick Actions 下载了示例项目这没问题,但是当我开始一个新的 Xcode 项目并复制它时,它对我不起作用。我肯定错过了什么。在这一点上,我只希望它在控制台中打印出“按下快捷方式”。当我添加 print("Shortcut pressed")到我下载的 Apple 项目,它工作正常。

在这一点上,我只是在尝试随机的事情。

我用一个数组、一个字典和字符串更新了我的 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.
    }


}

谢谢你。

最佳答案

看起来您的应用程序是基于场景的。对于基于场景的应用程序,您几乎可以忘记 AppDelegate并专注于SceneDelegate .您现在需要在 SceneDelegate 中覆盖两种方法。和一个在 AppDelegate .为清楚起见,我将模仿 Apple 的指南:

如果用户正在打开应用程序并且它是新启动的,您可以在 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
        }
    }

关于ios - 3D Touch 快速操作根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58458089/

相关文章:

javascript - WKWebview从iPhone的文档目录加载外部脚本JS

iphone - 获取位置和加速度信息

ios - 带有两个标签的水平 UIStackView(一个多行标签,一个单行)

ios - 私有(private)数据库中的 CloudKit 订阅

swift - 什么时候用xib,什么时候用ContainerView?

iOS 8 (Xcode 6.1.1) - 导航栏不像在 Storyboard 中那样工作

ios - 关于iPhone切换应用时更改状态栏颜色的问题

xcode - 无法调用非功能类型 '[Item]'的值

ios - 定义与 Swift 2.0 中的先前值冲突

swift - 即使在滚动时也将 MKPointAnnotation 修复到 MapKit 的中心