3D Touch Home Shortcuts In Obj-C 3D Touch Home Shortcuts In Obj-C ios ios

3D Touch Home Shortcuts In Obj-C


There are two states from where the user can open the app through Quick Actions.

TL;DRYou are always doing the same thing regardless of the state in which the app is when the quick action is done, that's why you only need to override application:performActionForShortcutItem:completionHandler: So if you wanted to do different things, then you would want to handle them in the two places, if not then just the overridden is enough.

  • One is if the app is killed or not running in background where we get the shortcut info on launch.

  • The other is if the app is running in background where we get the shortcut info on the new app delegate method.

To handle these Quick Action shortcuts when in background you need to override this method on App Delegate:

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler

And for not running in background (killed) on your

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

or

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions

you should check if the app was launched by a Quick Action:

UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];

(Link to related Apple Documentation)Quote from the Official Apple Docs

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.

If you find that your app was indeed launched using a quick action, perform the requested quick action within the launch method and return a value of NO from that method. When you return a value of NO, the system does not call the application:performActionForShortcutItem:completionHandler: method.


If you look at the sample code provided for apple, you'll see that they suggest that you write a method that handles your shortcut item so that you can handle it in all three places:

  • application: performActionForShortcutItem,
  • application: didFinishLaunchingWithOptions and
  • willFinishLaunchingWithOptions

An example of what I did was:

- (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {    BOOL handled = NO;    if (shortcutItem == nil) {        return handled;    }    if ([shortcutItem.type isEqualToString:kFavoritesQuickAction]) {        handled = YES;    }     if (handled) {        // do action here    }    return handled;}

Then you would just call this method in any place where you are getting a shortcut item. This should help you along your way!


Implement below 3 simple steps:

Step 1 : Write below method in AppDelegate class to Configure dynamic shortcut items.

NOTE : You can configure shortcut items in info.plist if you want it static.(Refer Apple documentation.)

/** *  @brief config dynamic shortcutItems *  @discussion after first launch, users can see dynamic shortcutItems */- (void)configDynamicShortcutItems {    // config image shortcut items    // if you want to use custom image in app bundles, use iconWithTemplateImageName method    UIApplicationShortcutIcon *shortcutSearchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];    UIApplicationShortcutIcon *shortcutFavoriteIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];    UIApplicationShortcutItem *shortcutSearch = [[UIApplicationShortcutItem alloc]                                                 initWithType:@"com.sarangbang.QuickAction.Search"                                                 localizedTitle:@"Search"                                                 localizedSubtitle:nil                                                 icon:shortcutSearchIcon                                                 userInfo:nil];    UIApplicationShortcutItem *shortcutFavorite = [[UIApplicationShortcutItem alloc]                                                 initWithType:@"com.sarangbang.QuickAction.Favorite"                                                 localizedTitle:@"Favorite"                                                 localizedSubtitle:nil                                                 icon:shortcutFavoriteIcon                                                 userInfo:nil];    // add all items to an array    NSArray *items = @[shortcutSearch, shortcutFavorite];    // add the array to our app    [UIApplication sharedApplication].shortcutItems = items;}

Step 2 : In AppDelegate class application didFinishLaunchingWithOptions method write below code.

    // UIApplicationShortcutItem is available in iOS 9 or later.        if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]){            [self configDynamicShortcutItems];            // If a shortcut was launched, display its information and take the appropriate action            UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];            if(shortcutItem)            {                // When the app launch at first time, this block can not called.                //App launch process with quick actions                [self handleShortCutItem:shortcutItem];            }else{                // normal app launch process without quick action            }        }

Step 3 : Write below delegate method and completion handler in AppDelegate class.

/* Called when the user activates your application by selecting a shortcut on the home screen, except when application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`. You should handle the shortcut in those callbacks and return `false` if possible. In that case, this callback is used if your application is already launched in the background. */- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{    BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];    completionHandler(handledShortCutItem);}/** *  @brief handle shortcut item depend on its type * *  @param shortcutItem shortcutItem  selected shortcut item with quick action. * *  @return return BOOL description */- (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{    BOOL handled = NO;    NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;    NSString *shortcutSearch = [NSString stringWithFormat:@"%@.Search", bundleId];    NSString *shortcutFavorite = [NSString stringWithFormat:@"%@.Favorite", bundleId];    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    if ([shortcutItem.type isEqualToString:shortcutSearch]) {        handled = YES;        SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"secondVC"];        self.window.rootViewController = vc;        [self.window makeKeyAndVisible];    }    else if ([shortcutItem.type isEqualToString:shortcutFavorite]) {        handled = YES;        ThirdViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"thirdVC"];        self.window.rootViewController = vc;        [self.window makeKeyAndVisible];    }    return handled;}