With the beginning of the iPhone 6s, Apple has introduced a 3D Touch mechanism which is very cool thing. The 3D Touch is also available on the newest iPhones 7. Nothing indicates that in the future Apple devices will run out of that feature so, here is a quick tutorial on how to improve your app using the one of the three main features of 3D Touch.
Add to your
Info.plist file a special key called
UIApplicationShortcutItems. It should be an array.
Add items to
UIApplicationShortcutItems array. Items should be a dictionary type.
Put info for each item.
There are few values to set here.
- UIApplicationShortcutItemType (required) - UIApplicationShortcutItemTitle (required) - UIApplicationShortcutItemSubtitle - UIApplicationShortcutItemIconType - UIApplicationShortcutItemIconFile - UIApplicationShortcutItemUserInfo
I will focus on the values that I set in my example app. The
UIApplicationShortcutItemType is a string that delivers an information to your app about what type of shortcut was pressed.
UIApplicationShortcutItemTitle is what user sees when the shortcut is shown.
UIApplicationShortcutItemIconType is a string that inform application what kind of system icon should be used for this shortcut.
And the last one is
UIApplicationShortcutItemIconFile defining the icon name that should be shown when the shortcut appears(instead of system icon).
Important thing here is that you can use system or your custom icons. Apple recommends that the custom icon should be square, single color, and 35x35 points.
UIApplicationShortcutItems keys and description you can get from Apple documentation.
Info.plist could look like that:
and by code:
Below you can see the result of above implementation.
Ok, now move to
AppDelegate class and let’s deal with the application’s shortcut tap events.
Let’s focus on this method:
That method is called every time when the application shortcut is pressed. You don’t have to worry about your application state. If your app is in background mode it just wakes up your app and triggers
performActionFor method. But if the application is terminated the app cycle will be
didLauchWithOptions and then
performActionFor shortcutItem will be called.
Of course, you can take the application shortcut in
didLauchWithOptions method by:
before it triggers the
It’s time to write some code here. So let’s assume that your
AppDelegate class includes these two methods:
Did you remember the
UIApplicationShortcutItemType? Value for this key is very useful to identify what application shortcut was tapped.
But on the
Info.plist file it occurs as a
String type and I strongly advise against comparing the string in their pure form.
Very helpful here might be
Enum type. So create the enum.
I think comparing types using enum is a much better way to identify what type of shortcut was tapped.
Now get back to the
The effect of implementation will look like this:
As you can see above I create two quick actions with custom icon, title and subtitle.
It is worth to mention about dynamic shortcut items. Yes, UIApplicationShortcutItems are split between
To add dynamic shortcut item you just have to add it to
UIApplication.sharedApplication().shortcutItems?.append(UIMutableApplicationShortcutItem(type: "my-dynamic-shortcut", localizedTitle: "Dynamic shortcut"))
The 3D Touch is powerful feature and makes iOS apps very nice to use. Interesting fact is that from the iOS 10 all apps available on AppStore have predefined
Share App shortcut. It allows us to share the applications without opening them. In my future posts I will describe more features that 3d touch gives us. So, thanks for reading and see you next time!
This post is cross-posted with my personal blog