9

我正在尝试将3D Touch 快捷方式添加到应用程序中,当在主屏幕上的应用程序图标上使用3DTouch时,我设法让快捷方式出现;但是,当使用快捷方式时,应用程序在加载时崩溃,我不确定为什么。

我已经设法让应用程序为书签快捷方式加载,但它不会启动BookmarksViewController,它只是加载InitialViewController.

该应用程序嵌入在每个选项卡的 aUITabBarController和 a中。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。

在此处输入图像描述 在此处输入图像描述

4

2 回答 2

5

我认为您忘记了 com.appName.Bookmark 中的“s”

在此处输入图像描述

或者您需要在此处从书签中删除“s”:

enum ShortcutItemType: String {

case Bookmarks
case Favourites
于 2016-01-11T06:56:40.520 回答
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" {

    }
}
于 2016-01-11T10:13:31.387 回答