【发布时间】:2016-01-06 14:39:58
【问题描述】:
我正在尝试将 3D Touch Shortcuts 添加到应用程序中,当在 主屏幕的应用程序图标上使用 3DTouch 时,我设法让快捷方式出现;但是,当使用快捷方式时,应用程序在加载时崩溃,我不确定为什么。
我已经设法让应用程序为书签快捷方式加载,但它不会启动BookmarksViewController,它只是加载InitialViewController。
应用程序嵌入在每个选项卡的UITabBarController 和UINavigationController 中。我尝试加载的两个视图控制器都位于不同的选项卡中,但导航控制器堆栈中的第一个视图。
有人知道我哪里出错了吗?
info.plist 文件
应用代理
enum ShortcutItemType: String {
case Bookmarks
case Favourites
init?(shortcutItem: UIApplicationShortcutItem) {
guard let last = shortcutItem.type.componentsSeparatedByString(".").last else { return nil }
self.init(rawValue: last)
}
var type: String {
return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)"
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
handleShortcutItem(shortcutItem)
}
return true
}
private func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {
if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
let sb = UIStoryboard(name: "main", bundle: nil)
let favouritesVC = sb.instantiateViewControllerWithIdentifier("FavouritesVC") as! FavouritesTableViewController
let bookmarksVC = sb.instantiateViewControllerWithIdentifier("BookmarksVC") as! BookmarksNotesViewController
switch shortcutItemType {
case .Bookmarks:
rootViewController.presentViewController(bookmarksVC, animated: true, completion: nil)
break
case .Favourites:
rootViewController.presentViewController(favouritesVC, animated: true, completion: nil)
break
}
}
}
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
handleShortcutItem(shortcutItem)
}
}
故事板 ID
这些是视图控制器的 StoryboardID。
【问题讨论】:
-
我对我的项目做了完全相同的事情,在检查了你的代码之后,唯一的区别是我在处理 appDidFinishLaunchWithOption 方法中的快捷方式项后“返回 false”,以防止它执行 performActionForShortcut 项方法。你可以试试看。
-
你能提供崩溃的堆栈跟踪吗?