Brian Coleman

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: 3D Touch – Quick Actions in Swift

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:
Screen Shot 2016-01-12 at 1.56.24 PM

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.

IMG_0018

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).

tvOS Tutorial: Top Shelf in SwiftTutorial: Testing SSL using Charles Proxy on an iOS Device
Brian Coleman

Manager, Mobile Development at Rogers Communications with over 15 years of multifaceted experience including development, design, business analysis and project management working directly with clients in a consulting capacity throughout the full software life cycle.

January 13, 2016 Swift, Tutorialsios9, swift, tutorial
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
Search
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.

FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2023 © Brian Coleman