9

I am trying to add 3D Touch Shortcuts to an application, I have managed to have the shortcuts appear when using 3DTouch on the app icon from the homescreen; however when using the shortcut the application crashes on load and I am unsure why.

I have managed to get the application to load for the bookmarks shortcut but it does not initiate the BookmarksViewController, it just loads the InitialViewController.

The application is embedded within a UITabBarController and a UINavigationController for each Tab. Both View Controllers I am trying to load are in different tabs but the first view in the navigation controller stack.

Does anyone know where I am going wrong ?

info.plist file

enter image description here

App Delegate

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

}

Storyboard ID

These are the StoryboardID for the View Controllers.

enter image description here enter image description here

RileyDev
  • 2,950
  • 3
  • 26
  • 61
  • I did exactly same thing with my project, after checking your code, the only difference is that I "return false" after handle the shortcut item in appDidFinishLaunchWithOption method to prevent it executing the performActionForShortcut item method. You may try it. – Surely Jan 06 '16 at 17:02
  • 2
    Can you provide stack trace for the crash? – Rafał Augustyniak Jan 09 '16 at 21:46

2 Answers2

5

I think you forgot the 's' in com.appName.Bookmark

enter image description here

Or you need to remove the 's' from Bookmarks here:

enum ShortcutItemType: String {

case Bookmarks
case Favourites
null
  • 1,178
  • 8
  • 15
2

Instead, try using a shortcut like this:

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

    }
}
John Hascall
  • 9,176
  • 6
  • 48
  • 72
RJE
  • 781
  • 6
  • 14