2016-01-06 134 views
9

我想3D觸控快捷鍵添加到應用程序,我已成功有捷徑出現;但是,當使用快捷方式時,應用程序在加載時崩潰,我不確定原因。3D觸控快捷鍵不起作用因意外碰撞

我已經設法讓應用程序加載書籤快捷方式,但它不啓動BookmarksViewController,它只是加載InitialViewController

該應用程序嵌入在每個Tab的UITabBarControllerUINavigationController之內。我試圖加載的兩個視圖控制器都在不同的選項卡中,但導航控制器堆棧中的第一個視圖。

有沒有人知道我要去哪裏錯了?

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

} 

故事板ID

這些都是StoryboardID的視圖控制器。

enter image description here enter image description here

+0

我確實做到了同樣的事情,與我的項目,檢查你的代碼後,唯一的區別是,我「返回false」處理快捷方式項後appDidFinishLaunchWithOption方法,以防止它執行performActionForShortcut項目方法。你可以試試。 – Surely

+2

你能提供崩潰的堆棧跟蹤嗎? –

回答

5

我想你忘記了 'S' 在com.appName.Bookmark

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" { 

    } 
}