Rehaan Advani
Rehaan Advani

Reputation: 975

3D Touch quick actions not working when app first launches

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.

Upvotes: 0

Views: 1004

Answers (1)

Cheng-Yu Hsu
Cheng-Yu Hsu

Reputation: 1049

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

Upvotes: 3

Related Questions