1

出于某种原因,我在找出我的应用程序中的快捷方式项目时遇到了一些麻烦。我已按照所有教程进行操作,并且我相信我做的一切都是正确的;但是,它的行为真的很奇怪。问题是当您完全关闭该应用程序并在主屏幕上执行 3D Touch Shortcut 项目时,它会将您带到应用程序的正确部分。但是,当应用程序在后台打开时(就像您只需单击主页按钮),它不会将您带到应用程序的正确部分,它只是打开它。这真的很奇怪,我不知道该怎么做,因为我已按照所有说明进行操作。太感谢了!

代码:

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {

    if shortcutItem.type == "com.myapp.newMessage" {

        let sb = UIStoryboard(name: "Main", bundle: nil)


        let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
         vc.selectedIndex = 2 //this takes me to the contacts controller



        let root = UIApplication.shared.keyWindow?.rootViewController

        root?.present(vc, animated: false, completion: {
            completionHandler(true)
        })


    } else if shortcutItem.type == "com.myapp.groups" {
        let sb = UIStoryboard(name: "Main", bundle: nil)


        let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
        vc.selectedIndex = 1 //this takes me to the groups controller

        let root = UIApplication.shared.keyWindow?.rootViewController

        root?.present(vc, animated: false, completion: {
            completionHandler(true)
        })

    } else {
        //more short cut items I will add later
    }
}

注意:这些是静态快捷方式项目,我在 info.plist 文件中对其进行了配置,此功能是应用程序中除了 info.plist 文件之外唯一包含有关快捷方式项目的地方。

编辑:这似乎是 RootViewController 的问题,因为我尝试了多种不同的方法,但它仍然给我同样的警告信息

2017-11-22 22:36:46.473195-0800 QuickChat2.0[3055:1068642] 警告:尝试呈现不在窗口层次结构中的视图!

我只是试图通过这篇文章以这种方式实现我的目标,但它给了我与以前完全相同的结果。

4

2 回答 2

1

您应该在您的变量中设置一个变量performActionForShortcutItem,告诉您快捷方式类型并将其中的代码移动到applicationDidBecomeActive. 你的可能应该是这样的

var shortcut: String?

func applicationDidBecomeActive(application: UIApplication) {
    if let shortcutItem = shortcut {
        shortcut = nil
        if shortcutItem == "com.myapp.newMessage" {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
            vc.selectedIndex = 2 //this takes me to the contacts controller
            let root = UIApplication.shared.keyWindow?.rootViewController
            root?.present(vc, animated: false, completion: nil)
        } else if shortcutItem == "com.myapp.groups" {
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let vc = sb.instantiateViewController(withIdentifier: "RecentVC") as! UITabBarController
            vc.selectedIndex = 1 //this takes me to the groups controller
            let root = UIApplication.shared.keyWindow?.rootViewController
            root?.present(vc, animated: false, completion: nil)
        } else {
            //more short cut items I will add later
        }
    }
}

func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    shortcut = shortcutItem.type
    completionHandler(true)
}
于 2017-11-23T05:31:39.663 回答
0

我认为更简洁的方法是在 performActionFor 方法中设置标志,然后在 VC 加载后检查它们。使用此通知将在应用程序每次激活时处理快捷方式,包括启动。

override func viewDidLoad() {
    super.viewDidLoad()
    // Sends notification if active app is reentered to handle quick actions
    NotificationCenter.default.addObserver(self, selector: #selector(handleQuickActions), name: UIApplication.didBecomeActiveNotification, object: nil)
于 2020-05-31T06:33:25.867 回答