Clicky

iOS Dev Nugget 157 Quick Actions to Launch Apps Using 3D Touch

.

Need to run a code review on your codebase? Hire me

iOS 9 introduced 3D Touch on the iPhone 6s and 6s Plus. There are a number ways you can utilize 3D Touch in your apps, but the easiest way is with Quick Actions. In Apple's own words:

Quick Actions let users do the things they do most often, faster and in fewer steps. Many of these actions can even be done with a single press, right from the Home screen.

It's very simple to integrate. You can add one or more static entries in your Info.plist file:

<key>UIApplicationShortcutItems</key>
<array>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>com.example.appname.write</string>
        <key>UIApplicationShortcutItemIconType</key>
        <string>UIApplicationShortcutIconTypeCompose</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Write</string>
    </dict>
    <dict>
        <key>UIApplicationShortcutItemType</key>
        <string>com.example.appname.postpicture</string>
        <key>UIApplicationShortcutItemIconType</key>
        <string>UIApplicationShortcutIconTypeCompose</string>
        <key>UIApplicationShortcutItemTitle</key>
        <string>Post Picture</string>
    </dict>
</array>

You then implement -application:performActionForShortcutItem:completionHandler: from UIApplicationDelegate, check for the action to handle and call completionHandler once you are done. You may also want to check -application:didFinishLaunchingWithOptions: and inspect launchOptions for the UIApplicationLaunchOptionsShortcutItemKey key to see if the app is launched from a quick action and perhaps handle things differently (e.g. returning NO, so that -application:performActionForShortcutItem:completionHandler: doesn't get called).

In addition to static shortcuts, you can add dynamic ones by assigning to the shortcutItems property of UIApplication.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues