快捷物品,快速动作3D Touch swift

时间:2017-11-23 04:47:38

标签: ios swift 3dtouch quickaction uiapplicationshortcutitem

我出于某种原因在我的应用程序中弄清楚了快捷方式项目时遇到了一些麻烦。我已经完成了所有的教程,我相信我已经做好了一切;然而,它表现得非常奇怪。问题是,当您完全关闭该应用程序并在主屏幕上执行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]警告:尝试在窗口层次结构中显示其视图!

我只是试图以这种方式完成我的目标through this post,但它给了我与以前完全相同的结果。

2 个答案:

答案 0 :(得分:0)

您应该在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)
}

答案 1 :(得分: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)
相关问题