3D Touch shortcuts won't work due to Unexpected Crash
Asked Answered
C

2

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

Cogen answered 6/1, 2016 at 14:39 Comment(2)
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.Carlyle
Can you provide stack trace for the crash?Wenona
V
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
Variolous answered 11/1, 2016 at 6:56 Comment(0)
J
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" {

    }
}
Jesselton answered 11/1, 2016 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.