【问题标题】:3D Touch shortcuts won't work due to Unexpected Crash由于意外崩溃,3D Touch 快捷方式无法使用
【发布时间】: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 项方法。你可以试试看。
  • 你能提供崩溃的堆栈跟踪吗?

标签: ios swift 3dtouch


【解决方案1】:

我想你忘记了 com.appName.Bookmark 中的“s”

或者您需要在此处从书签中删除“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" {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多