implementation of 3D Touch in iOS

Leela Krishna
Swift India
6 min readAug 5, 2018

--

There are 4 different ways to use the 3D Touch in iOS:

  • Home screen quick actions.
  • UIKit peek and pop actions.
  • Web view peek and pop actions.
  • UITouch force properties.

in this tutorial we will go through the first one, Home screen quick actions.

→ Home Screen Quick Actions

Apple has added a nice feature in iOS 9, known as Home-screen quick actions. These quick actions to the app icon on the iOS device help the user to navigate to the particular part or screen of the app. This feature is already there in mac and Apple watch with the name Force Touch. in iOS it is renamed as 3D Touch, as they made some changes to distinguish multiple levels of touches based on how firmly you press.

We can change the sensitivity of this 3d touch in the iOS device settings → General → Accessibility → 3D Touch. New devices From iPhone 6s, 6S Plus…iPhone X have 3D Touch available.

When the user selects a quick action, your app will be launched (if not running currently), or else if your app is already launched and it is in background/suspended state your app will be activated before the app delegate object receives the quick action message or notification. We can have up to max 4 shortcut actions for any app, and a fifth one called shareApp will be added by the system if it is downloaded from the app-store.

We can have both static and dynamic quick actions in our app, but static actions have high priority then dynamic actions. So if your app has 3 static and 3 dynamic quick action shortcuts, only one dynamic action will be presented to the user along with those 3 static ones.

→ Start Coding :

To add Quick Actions, In your app’s Info.plist file create a UIApplicationShortcutItems’ array. Each element of this array is a dictionary containing the properties(like the title, subtitle, icon of the action, type(a unique identifier to your shortcut) of the action) of one quick action.

Description of the keys:

  • UIApplicationShortcutItemType: A required String that identifies your Quick Action. The String must be unique and app specific. A good idea is to prefix it with your bundle ID or some other app unique String. if you want to use your app bundle identifier, use place holder like this which gives the path for your bundle identifier. “$(PRODUCT_BUNDLE_IDENTIFIER)”. So even in future if you change the bundle id, your app shortcuts do not effect, and you do not need to change your bundle id manually in the other places like this.
  • UIApplicationShortcutItemTitle: A required String that displays the title of the Quick Action once the user presses the icon of the app. If your app is Localised, you can set the String key of your Localized.string file. This is showed to user on quick actions.
  • UIApplicationShortcutItemSubtitle: An optional String that displays the subtitle of the Quick Action. The subtitle can also be localized as the title.
  • UIApplicationShortcutItemIconType: An optional String that displays a system icon provided by the library. Check the UIApplicationShortcutIcon enumeration to see all the possible string keys.
  • UIApplicationShortcutItemIconFile: An optional String that displays a custom icon image to use from the app’s bundle, or the name of an image in an asset catalog.
  • UIApplicationShortcutItemUserInfo: A Dictionary containing some extra information you want to pass with the Quick Action.

The quick actions are presented to the user on the iOS home screen, so on selection of these options we need to handle them in our app. The best place to handle them is your App-delegate class. in this app i used an enum for UIApplicationShortcutItemType to avoid the typing errors. Whenever the user selected the quick action on the home-screen, in the app’s App-delegate class, application:shortcutItem:completionHandler: method will be called. This method must call the completion handler with a boolean value depending on a success or failure of the Quick Action.

In application:didFinishLaunchingWithOptions: method we are saving that value into launchedShortcutItem so can we handle it next.

The next method to get called is applicationDidBecomeActive, this method gets called after application:didFinishLaunchingWithOptions: during the first launch of your app, or every time the user comes into your app while it’s still open in the background.

When a user chooses one of the quick actions , the app launches or resumes. So, either of the above two methods (application:didFinishLaunchingWithOptions: or applicationDidBecomeActive) will be called before the performActionForShortcutItem method calls. As we discussed above, This is the required method to handle the shortcut actions by calling its completionHandler(Bool) with true or false.

In the following image, I have defined an enum for the shortcut identifiers for my quick actions. I used this enum type to find out the which action has been triggered by the user in the handleShortcutItem method.

In the following image, we defined our handelShortcutItem method. First we checked the triggered shortcut action’s type is nil or not. Then got the triggered type. After using the switch case, the required functionality is performed.

→ Dynamic Shortcut Actions : So far we have seen static actions which we configured in app’s info.plist file and Then in AppDelegate class. We can implement the dynamic actions as well, but remember only 4 actions are shown, with high priority for static actions. Let’s see the configuring dynamic actions.

In the above image, I configured the dynamic action through code. First I set the type, remember it must be unique and required one, so as like before I assigned the type with app’s bundle identifier as prefix. Then I initialised the shortcut item by API. in this one you can give the title and sub title dynamically as you need(icon as well). Finally I assigned the created item to the UIApplication’s shortcutItems array. You can call the above snippet which I wrapped in a function wherever you needed. I called this in the ViewController’s viewDidLoad method.

in the AppDelegate class, I wrote another function to perform this dynamic action. You can see it is self explanatory and very simple.

At last I modified the handleShortcutItem function to perform the dynamic action. check the following code(in image)

Now you have seen how easy it is to implement Home-screen shortcut actions for an iOS mobile app. Enjoy coding!!!

You can download the sample project here.

— — — — — — — — — *********************** — — — — — — — — —

If you like my tutorials please follow me on medium, twitter & linkedIn accounts.

Thanks for reading…

****************************!!!See you!!!****************************

--

--