qwerty
qwerty

Reputation: 221

UIApplicationShortcutItem in didFinishLaunching

According to Apple documentation :

It’s your responsibility to ensure the system calls this method conditionally, depending on whether or not one of your app launch methods (application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions:) has already handled a quick action invocation. The system calls a launch method (before calling this method) when a user selects a quick action for your app and your app launches instead of activating. The requested quick action might employ code paths different than those used otherwise when your app launches. For example, say your app normally launches to display view A, but your app was launched in response to a quick action that needs view B. To handle such cases, check, on launch, whether your app is being launched via a quick action. Perform this check in your application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method by checking for the UIApplicationLaunchOptionsShortcutItemKey launch option key. The UIApplicationShortcutItem object is available as the value of the launch option key.

But why there is a need to handle this in didfinishlauncing/willfinishLauncing methods. If the app is in killed state, eventually it will call the performActionForShortcutItem method, so why there is a need to check in didfinish method?

Sample code:

//code




    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        var launchedFromShortCut = false
        //Check for ShortCutItem
        if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
            launchedFromShortCut = true
            handleShortCutItem(shortcutItem)
        }

        //Return false incase application was lanched from shorcut to prevent
        //application(_:performActionForShortcutItem:completionHandler:) from being called
        return !launchedFromShortCut
    }

    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
        let handledShortCutItem = handleShortCutItem(shortcutItem)
        completionHandler(handledShortCutItem)
    }

    func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
        var handled = false
        //Get type string from shortcutItem
        if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
            //Get root navigation viewcontroller and its first controller
            let rootNavigationViewController = window!.rootViewController as? UINavigationController
            let rootViewController = rootNavigationViewController?.viewControllers.first as UIViewController?
            //Pop to root view controller so that approperiete segue can be performed
            rootNavigationViewController?.popToRootViewControllerAnimated(false)

            switch shortcutType {
            case .Torquiose:
                rootViewController?.performSegueWithIdentifier(toTurquoiseSeque, sender: nil)
                handled = true
            case.Red:
                rootViewController?.performSegueWithIdentifier(toRedSeque, sender: nil)
                handled = true
            }
        }
        return handled
    }
}

Upvotes: 2

Views: 591

Answers (1)

Alex
Alex

Reputation: 60

It gives you the flexibility to decide - you can handle it "early" in didFinishLaunching - returning FALSE will inhibit performActionForShortcutItem from getting called later. Or you can return TRUE in didFinishLaunching, and performActionForShortcutItem will still get called.

Upvotes: 0

Related Questions