3D Touch快捷方式由于意外崩溃而无法正常工作。

9
我正在尝试为一个应用程序添加3D Touch快捷方式。当我在主屏幕上使用3DTouch来打开应用程序时,快捷方式会出现;然而,当我使用快捷方式时,应用程序会在加载时崩溃,我不确定原因。
我已经成功让书签快捷方式加载应用程序,但它没有启动BookmarksViewController,只加载了InitialViewController。
该应用程序嵌入在UITabBarController中,并且每个标签都有UINavigationController。我尝试加载的两个视图控制器位于不同的标签中,但是都是导航控制器堆栈中的第一个视图。
有人知道我错在哪里吗?
info.plist文件

enter image description here

应用程序代理

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

}

故事板标识符

这些是视图控制器的故事板标识符。

enter image description here enter image description here


我在我的项目中也做了同样的事情,在检查了你的代码之后,唯一的区别是我在appDidFinishLaunchWithOption方法中处理完快捷方式项后添加了"return false"以防止执行performActionForShortcut方法。你可以试试看。 - Surely
2
你能提供崩溃的堆栈跟踪吗? - Rafał Augustyniak
2个回答

5
我认为您忘记了在com.appName.Bookmark中加入's'。

enter image description here

或者你需要在这里删除“书签”中的“s”:

enum ShortcutItemType: String {

case Bookmarks
case Favourites

2

相反,尝试使用这样的快捷方式:

if shortcutItem.type == "com.appName.Bookmark"
@available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    if shortcutItem.type == "com.appName.Bookmark" {

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接