0

I am having some problem with 3D Touch. It works perfectly, except when the app is first launched. Here is the following code in my AppDelegate:

 func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var quickActionHandled = false
    let type = shortcutItem.type.componentsSeparatedByString(".").last!
    if let shortcutType = Shortcut(rawValue: type) {
        switch shortcutType {
        case .aboutMe:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.aboutMeTap), name: "about", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("about", object: self)
            quickActionHandled = true
        case .education:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.educationTap), name: "education", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("education", object: self)
            quickActionHandled = true
        case .projects:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.projectsTap), name: "projects", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("projects", object: self)
            quickActionHandled = true
        case .why:
            NSNotificationCenter.defaultCenter().addObserver(RAIntroductionViewController(), selector: #selector(RAIntroductionViewController.whyPressed(_:)), name: "why", object: nil)
            NSNotificationCenter.defaultCenter().postNotificationName("why", object: self)
            quickActionHandled = true
        }
    }
    return quickActionHandled
}

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    completionHandler(handleQuickAction(shortcutItem))

}

Here is the code in the viewDidLoad method of the RAIntroductionViewController:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.aboutMeTap), name: "about", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.educationTap), name: "education", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.projectsTap), name: "projects", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.whyPressed(_:)), name: "why", object: nil)

Those observers call methods that push the navigation controller.

While everything works flawlessly, the navigation controller is not pushed to the right page when the app is first launch. It simply goes to the root view controller.

Rehaan Advani
  • 945
  • 1
  • 12
  • 24

1 Answers1

3

When the app is first launched, the application:performActionForShortcutItem shortcutItem:completionHandler: is not called.

Instead, you should handle this in application:didFinishLaunchingWithOptions:.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
    if let options = launchOptions,
       let shortcutItem = options[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
       // do the work here..
    }
    return true
}

Hope this helps :)

Cheng-Yu Hsu
  • 1,029
  • 7
  • 11
  • Thank you for the response. I tried this out, but I get an error: unexpectedly found nil while unwrapping an optional value. I'm assuming that the `shortcutItem` variable is nil. Any ideas? – Rehaan Advani Apr 30 '16 at 03:40
  • Good to hear feedback from you, the code only works when `launchOptions` is not `nil` and there's a value for `UIApplicationLaunchOptionsShortcutItemKey`. I've updated the code so the body of the `if` statement will be execute only if the app is launched from 3D Touch shortcut. Would you mind trying the new code and see if it work? Thanks! – Cheng-Yu Hsu Apr 30 '16 at 04:50
  • Thank you for your update. I tried this code, and nothing happened. There were no errors or crashes, but it did not go to the correct page. Just to clarify, I am using NSNotificationCenter to add an observer to do this. – Rehaan Advani Apr 30 '16 at 04:55
  • I inserted a print statement in the if statement, and it is not even executed. – Rehaan Advani Apr 30 '16 at 05:10
  • Would you mind if we discuss this in chat and figure out the solutions together? https://chat.stackoverflow.com/rooms/110695/3d-touch-quick-actions-not-working-when-app-first-launches – Cheng-Yu Hsu Apr 30 '16 at 05:20