As of iOS 9, Apple has added quick actions to the app icon so users can deep link into an area of your app quicker. By pressing the app icon of the latest devices including the iPhone 6s and iPhone 6s Plus, the user obtains a set of quick actions. When the user selects a quick action, your app activates or launches and your app delegate object receives the quick action message.
Define Quick Actions
In your app’s Info.plist file create a UIApplicationShortcutItems array. This is where we define what the actions are, the title, subtitle, and short cut keys for each. Note that you can only have a max of 4 quick actions off the icon in your app.
UIApplicationShortcutItemType – A required string delivered to your app when the user invokes the corresponding quick action.
UIApplicationShortcutItemTitle – A required string displayed to the user on the Home screen as the name of the quick action.
UIApplicationShortcutItemSubtitle – An optional string that is displayed to the user on the Home screen, immediately below the corresponding title string.
UIApplicationShortcutItemIconType – An optional string specifying the type of an icon from the system-provided library.
You can find a full list of the icons Apple has in their asset library here:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationShortcutIcon_Class/index.html#//apple_ref/c/tdef/UIApplicationShortcutIconType
UIApplicationShortcutItemIconFile – An optional string specifying an icon image to use from the app’s bundle, or the name of an image in an asset catalog.
UIApplicationShortcutItemUserInfo – An optional, app-defined dictionary. One use for this dictionary is to provide app version information.
Below is an example of the ones I created for our sample app:
Next you can run your app and test to see if the quick actions are formatted the way you expected them to look. Note: You must develop on a device that supports 3D Touch. The simulator in Xcode does not support 3D Touch.
Handle the Short Cuts
Begin by adding the enum and properties we are going to need in the methods below. If you don’t want to use enums, then make sure your names match the UIApplicationShortcutItemType values entered in your Info.plist.
enum ShortcutIdentifier: String { case First case Second case Third case Fourth // MARK: Initializers init?(fullType: String) { guard let last = fullType.componentsSeparatedByString(".").last else { return nil } self.init(rawValue: last) } // MARK: Properties var type: String { return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)" } } /// Saved shortcut item used as a result of an app launch, used later when app is activated. var launchedShortcutItem: UIApplicationShortcutItem?
Read in the UIApplicationShortcutItem that is selected by the User in the didFinishLaunchingWithOptions method. Here we are saving that value into launchedShortcutItem so can we handle it next.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. // If a shortcut was launched, display its information and take the appropriate action if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { launchedShortcutItem = shortcutItem } return true }
The next method to get called is applicationDidBecomeActive, this method gets called after 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.
func applicationDidBecomeActive(application: UIApplication) { guard let shortcut = launchedShortcutItem else { return } handleShortCutItem(shortcut) launchedShortcutItem = nil }
When a user chooses one of the quick actions the app launches or resumes the app and calls the performActionForShortcutItem method below in your app delegate.
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) { let handledShortCutItem = handleShortCutItem(shortcutItem) completionHandler(handledShortCutItem) }
Lastly we need to handle the short cut and deep link the user into the proper view controller within our app.
func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool { var handled = false // Verify that the provided `shortcutItem`'s `type` is one handled by the application. guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false } guard let shortCutType = shortcutItem.type as String? else { return false } let storyboard = UIStoryboard(name: "Main", bundle: nil) var vc = UIViewController() switch (shortCutType) { case ShortcutIdentifier.First.type: // Handle shortcut 1 vc = storyboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 handled = true break case ShortcutIdentifier.Second.type: // Handle shortcut 2 vc = storyboard.instantiateViewControllerWithIdentifier("VC2") as! ViewController2 handled = true break case ShortcutIdentifier.Third.type: // Handle shortcut 3 vc = storyboard.instantiateViewControllerWithIdentifier("VC3") as! ViewController3 handled = true break case ShortcutIdentifier.Fourth.type: // Handle shortcut 4 vc = storyboard.instantiateViewControllerWithIdentifier("VC4") as! ViewController4 handled = true break default: break } // Display the selected view controller window!.rootViewController?.presentViewController(vc, animated: true, completion: nil) return handled }
You can grab the full source code for this tutorial. Note: Created using Xcode 7.2 (Swift 2).