首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >3D Touch Quick actions根本不起作用

3D Touch Quick actions根本不起作用
EN

Stack Overflow用户
提问于 2019-10-19 04:55:32
回答 1查看 1.3K关注 0票数 5

我已经检查了所有其他问题,但我找不出哪里出了问题。我从苹果开发人员网站Add Home Screen Quick Actions下载了示例项目,没有问题,但是当我开始一个新的Xcode项目并复制它时,它对我不起作用。我一定是漏掉了什么。在这一点上,我只想让它在控制台上打印出“快捷键按下”。当我将print("Shortcut pressed")添加到我下载的苹果项目中时,它工作得很好。

在这一点上,我只是在尝试一些随机的东西。

我用一个数组、一个字典和字符串更新了我的info.plist,我只是复制并传递了这些值,以免出现任何打字错误。

UIApplicationShortcutItems,项目0,UIApplicationShortcutItemType,UIApplicationShortcutItemIconType,UIApplicationShortcutItemTitle

按下应用程序图标时会出现快捷方式,但它只会打开应用程序。

这是我最基本的AppDelegate.Swift文件,只是试图让它做任何事情它可能是我的项目设置,我的Xcode版本是最新的-版本11.1 (11A1027)

我以前从来没有使用过Quick actions,它看起来很简单,但看起来很简单,只需在plist中添加一些行,并在AppDelegate.Swift文件中添加一些代码,但要花很长时间才能开始工作。

代码语言:javascript
运行
复制
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.
    }


}

谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-20 14:13:46

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

如果用户正在打开应用程序,并且是新启动的应用程序,您可以在AppDelegate中处理此问题

代码语言:javascript
运行
复制
     // 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中进行处理

代码语言:javascript
运行
复制
    // 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
    }

场景准备就绪后,您可以使用快捷键执行所需的操作:

代码语言:javascript
运行
复制
    // 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
        }
    }
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58458089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档