3D Touch in iOS 9, Part 4: Adding Static Shortcut Items

IMG_0121

The array of shortcut items can also be added to our app’s Info.plist. The result is almost the same as generating this array in code (as discussed in my previous article): the difference is that static shortcuts are available as soon as the app is installed, even if the user hasn’t opened it yet. With dynamic shortcuts, the app needs to be opened at least once for the shortcuts to be populated.

To get started, select your app’s Info.plist file in Xcode. If you’ve never used the plist editor before, you’re in for a little culture shock (because it’s neither fun nor intuitive to use). Here’s what you do step by step.

Add a new row to the file by right-clicking at the bottom of the list. Call the new row UIApplicationShortcutItems and turn it into an array. Notice that little triangle at the beginning of the row. Click to open it. Now hover over your new row and click on the plus icon that appears. This will create a new item in our array (whose name you can’t change). Hover over Item 0 and turn it into a dictionary, then open it with the disclosure triangle that has popped up.

This item will define your first shortcut. Click the plus icon in Item 0 to add a new row for each key and enter the following (or as many as you need):

  • UIApplicationShortcutItemTitle
  • UIApplicationShortcutItemSubtitle
  • UIApplicationShortcutItemType
  • UIApplicationShortcutItemIconFile

Give each of these an appropriate value until you get something like this:

Screen Shot 2015-09-28 at 07.30.34

Rinse and repeat for as many static shortcuts as you need. You have several other keys at your disposal, read all about those in Apple’s Info Plist Documentation.

Mixing with Dynamic Shortcuts

You can use both dynamic and static shortcuts if you like. In my previous article about dynamic shortcuts we’ve already seen how these are created. In order not to overwrite existing static shortcuts though, we need to add our dynamic shortcuts to the existing array we’ve just created rather than wipe it out (as we did before).

We can do this by using the arrayByAddingObjectsFromArray method, combining both arrays:

- (void)createItemsWithIcons {
    
    ...
    
    // add this array to the potentially existing static UIApplicationShortcutItems
    NSArray *existingItems = [UIApplication sharedApplication].shortcutItems;
    NSArray *updatedItems = [existingItems arrayByAddingObjectsFromArray:items];
    [UIApplication sharedApplication].shortcutItems = updatedItems;

}

We react to static shortcuts just like we did to dynamic ones. I’ve explained how to do it in my previous article.

Demo Project

I’ve added all this static goodness to my demo project on GitHub:





3 thoughts on “3D Touch in iOS 9, Part 4: Adding Static Shortcut Items

  1. Hi thank you for this tutorial its really good.
    Do you know how I have to call my view (like your DeepLink2 view) when my UINavigationController is inside a UITabBarController?

    UITabBarController->UINavigationBarControler->DeepLink1-> DeepLink2

    1. Hi wee7ee,

      in that case, instantiate the tab bar controller first, then tell it which tab you’d like selected. Next, grab your nav controller and push your chosen view controller onto the nav stack. Here’s what it looks like in code:

      // grab our storyboard
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
      
      // instantiate tab bar controller
      UITabBarController *tab = [storyboard instantiateViewControllerWithIdentifier:@"Tab"];
      
      // tell tab bar which tab to make visible
      [tab setSelectedIndex:1];
      
      // push second view controller onto the nav stack
      UINavigationController *nav = [tab.viewControllers objectAtIndex:1];
      UIViewController *view2 = [storyboard instantiateViewControllerWithIdentifier:@"View2.2"];
      [nav pushViewController:view2 animated:NO];
      
      // and make it visible
      self.window.rootViewController = tab;
      [self.window makeKeyAndVisible];
      

      Note that tabs begin with 0 on the left. So in this example, we’re selecting the second tab (index 1), onto which the second view controller appears in place on the navigation stack.

      Hope this helps 🙂

Leave a Reply