Implementing 3D Touch on iPhone 6s — Quick Actions, Quickly!

Apple is always known for innovation. With the launch of iPhone 6s and iPhone 6s plus apple has added a pressure sensitive layer on the their screen. Addition of this useful hardware in the devices has created lot of UX possibilities!

It’s possible to do a hard press on an application icon and get shortcuts which get you to a specific point in your app. For example, if you press hard on the Photo’s app icon you can quickly search for an image, check the most recent images or see your favourites. It’s also possible to make these quick actions dynamic, meaning that you can add and remove actions based on the state of your application.

So how to implement this cool feature? I will try to explain in brief.

Setup Shortcuts

Step 1. Open your Xcode Project and open the info.plist file. And add values fro UIApplicationShortcutItems. The value will be an array of shortcut items.

The final info.plist will look like:

The info.plist file of project showing ShortcutItem

If you notice each UIApplicationShortcutItem has some properties. Here is the class declaration of the class:

These shortcut items represent each item which will be displayed on iPhone’s screen. So this is how a Shortcuts look like :

Quick Actions, deep press home screen

You can see the Quick Actions appear after applying force touch on Home screen. There are default icon types also which we can define by the key UIApplicationShortcutItemIconType of UIApplicationShortcutItem, the enum for the same is

Declaration

enum UIApplicationShortcutIconType : Int { case Compose case Play case Pause case Add case Location case Search caseShare }

Here are more details on it: UIApplicationShortcutItemTypes

Testing

Now this was pretty easy, wasn’t it? There is a challenge what developers face is while testing your changes. iOS simulators do not support 3D Touch! You can implement these changes however you will be unable to see it working unless you have a real iPhone 6s device. So, for this there is some useful repository by DeskConnect called SBShortcutMenuSimulator. This repo will help you simulate the force tap on an app icon on Home screen. The instructions are given in there repository however the three main commands you will be using are

  1. xcrun simctl spawn booted launchctl debug system/com.apple.SpringBoard — environment DYLD_INSERT_LIBRARIES=[PWD]/SBShortcutMenuSimulator.dylib
  2. xcrun simctl spawn booted launchctl stop com.apple.SpringBoard
  3. echo ‘com.suhailrashidbhat.SRBShortcutsDemo’ | nc 127.0.0.1 8000

After you setup SBShortcutMenuSimulator by command make. You can run the app by Xcode and then run #1 command. When the simulator is booted up and running it will show a message

Service configured for next launch.

Then run command #2 and it will relaunch the simulator. Now the setup is done to simulate force press you can use command #3 to simulate it on the app with identifier ‘com.suhailrashidbhat.SRBShortcutsDemo’ . You have to change this to your app bundle identifier.

Handling selected shortcut

When using a Quick Action, the app should land on the corresponding view controller in the app. Yea, I think you guess it right didFinishLanchingWithOptions, the way push notifications work. didFinishLanchingWithOptions method gets called when the app is launched and options dictionary has the data about the UIApplicationShortcutItem.

You can check the information in Options dictionary and make the right choice of view controller to launch or any other intended action.

There is another delegate method added:

- application:performActionForShortcutItem:completionHandler:

Called when the user selects a Home screen quick action for your app, except when you’ve intercepted the interaction in a launch method.

optional func application(_ application: UIApplication,
performActionForShortcutItem shortcutItem:
UIApplicationShortcutItem, completionHandler completionHandler: (Bool) -> Void)

Using this method helps to perform action on shortcut selections when app is in background state and a Quick Action is selected.

Reference: https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/Adopting3DTouchOniPhone/

Sample Project:

Download Sample Code

Show your support

Clapping shows how much you appreciated Suhail Bhat’s story.