3D Touch快速操作根本不起作用 [英] 3D Touch Quick actions not working at all

查看:55
本文介绍了3D Touch快速操作根本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了所有其他问题,但我不知道出了什么问题.我从Apple开发人员网站添加主屏幕快速操作中下载了示例项目那没问题,但是当我启动一个新的Xcode项目并复制它时,对我来说这是行不通的.我肯定错过了什么.在这一点上,我只希望它在控制台中显示快捷方式已按下".当我将 print(按下快捷键")添加到我下载的Apple项目中时,它运行良好.

Ive been through all the other questions and I can't figure out what's wrong. I downloaded the sample project from the Apple Developer site Add Home Screen Quick Actions and that works no problem, but when I start a new Xcode project and copy that exactly it doesn't work for me. I must be missing something. At this point I just want it to print in the console saying "shortcut pressed". When I add print("Shortcut pressed") to the Apple project I downloaded it works fine.

这时我只是尝试随机的东西.

At this point I am just trying random things.

我用数组,字典和字符串更新了info.plist,我只是复制并粘贴了这些值,以免造成任何错字错误.

I updated my info.plist with an Array and a dictionary and strings and I just copy and past the values as to not make any typo mistakes.

UIApplicationShortcutItems,项目0,UIApplicationShortcutItemType,UIApplicationShortcutItemIconType,UIApplicationShortcutItemTitle

UIApplicationShortcutItems, Item 0, UIApplicationShortcutItemType, UIApplicationShortcutItemIconType, UIApplicationShortcutItemTitle

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

The shortcut appears when pressing the app icon but it just opens the app.

这是我非常基本的AppDelegate.Swift文件,只是试图使其执行任何操作可能是我的项目设置,我的Xcode版本是最新的-版本11.1(11A1027)

This is my very basic AppDelegate.Swift file just trying to get it to do anything Could it be my project settings, my version of Xcode is up to date - Version 11.1 (11A1027)

我以前从未使用过快速操作,它接缝很简单,但是接缝很简单,只需在plist中添加一些行,然后将一些代码添加到AppDelegate.Swift文件中,但要花很多时间才能开始工作.

Ive never used Quick actions before and it seamed simple but what seamed simple, just add some lines to the plist and add some code to the AppDelegate.Swift file but is taking ages to get working.

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的指南:

Looks like your app is Scene based. For Scene based apps you can almost forget about the AppDelegate and focus on the SceneDelegate. There are two methods you now need to override in the SceneDelegate and one in the AppDelegate. I'll mimic Apple's guide for clarity:

如果用户正在打开应用程序并且是新启动,则可以在 AppDelegate 中处理此操作:

If the user is opening the app and it's a fresh launch, you handle this in the 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 中处理该操作:

If your app is still running in the background when the user clicks on the shortcut item, you handle that in the 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
    }

场景准备好后,您可以使用快捷方式执行所需的操作:

Once the scene is ready, you can do what you need to with the shortcut :

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

这篇关于3D Touch快速操作根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆