出于某种原因,我在找出我的应用程序中的快捷方式项目时遇到了一些麻烦。我已按照所有教程进行操作,并且我相信我做的一切都是正确的;但是,它的行为真的很奇怪。问题是当您完全关闭该应用程序并在主屏幕上执行 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] 警告:尝试呈现不在窗口层次结构中的视图!
我只是试图通过这篇文章以这种方式实现我的目标,但它给了我与以前完全相同的结果。