Skip to content

maximbilan/3D-Touch-Quick-Actions-Demo

Repository files navigation

iOS 3D Touch: How to add quick actions

Recently Apple has provided a new technology 3D Touch for iPhone 6S and 6S Plus. Which contains 3 new features for your applications.

alt tag

Quick Actions

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.

alt tag

Peek and Pop

Let your users preview all kinds of content and even act on it — without having to actually open it. Users can then press a little deeper to Pop into content in your app.

alt tag

Pressure Sensitivity

Creative apps can take advantage of the pressure-sensing display of iPhone 6 sand iPhone 6s Plus in many ways. For example, they can vary line thickness or give a brush a changing style.

alt tag

More information you can of course found on Apple. Guides, tutorials, examples, etc.

I would like to tell about Quick Actions, how to implement in application. Actions can be static or dynamic. Let’s start.

First of all, we need to add UIApplicationShortcutItems to Info.plist. Each child item should be a dictionary and must have at least these required keys:

UIApplicationShortcutItemType: a string which is sent to your application as a part of UIApplicationShortcutItem. It can be used in code to handle actions for different shortcut types.

UIApplicationShortcutItemTitle: a title of your action. Can be localized.

And there are optional keys:

UIApplicationShortcutItemSubtitle: a subtitle of your action. Also can be localized.

UIApplicationShortcutItemIconType: an optional string which defines built-in icon type:

enum UIApplicationShortcutIconType : Int {
  case Compose
  case Play
  case Pause
  case Add
  case Location
  case Search
  case Share
  case Prohibit
  case Contact
  case Home
  case MarkLocation
  case Favorite
  case Love
  case Cloud
  case Invitation
  case Confirmation
  case Mail
  case Message
  case Date
  case Time
  case CapturePhoto
  case CaptureVideo
  case Task
  case TaskCompleted
  case Alarm
  case Bookmark
  case Shuffle
  case Audio
  case Update
}

UIApplicationShortcutItemIconFile: an optional string specifying an image from Assets Catalog or from the Bundle.

UIApplicationShortcutItemUserInfo: an optional dictionary of additional user information. Let’s try. Please add the next data to Info.plist:

<key>UIApplicationShortcutItems</key>
<array>
  <dict>
    <key>UIApplicationShortcutItemIconType</key>
    <string>UIApplicationShortcutIconTypeShare</string>
    <key>UIApplicationShortcutItemTitle</key>
    <string>SHORTCUT_TITLE_SHARE</string>
    <key>UIApplicationShortcutItemType</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER).Share</string>
  </dict>
  <dict>
    <key>UIApplicationShortcutItemIconType</key>
    <string>UIApplicationShortcutIconTypeAdd</string>
    <key>UIApplicationShortcutItemTitle</key>
    <string>SHORTCUT_TITLE_ADD</string>
    <key>UIApplicationShortcutItemType</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER).Add</string>
  </dict>
</array>

Also create InfoPlist.strings:

"SHORTCUT_TITLE_SEARCH" = "Search";
"SHORTCUT_TITLE_FAVORITES" = "Favorites";

And what we got:

alt tag

Let’s implement handling of shortcuts. We need to create the next enumeration:

enum ShortcutIdentifier: String {
  case Share
  case Add
 
  init?(fullType: String) {
    guard let last = fullType.componentsSeparatedByString(".").last else { return nil }
    self.init(rawValue: last)
  }
  
  var type: String {
    return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)"
  }
}

And a method for handling UIApplicationShortcutItem. For example:

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 }

  switch (shortCutType) {
    case ShortcutIdentifier.Share.type:
      // Handle shortcut 1 (static).
      handled = true
    break
    case ShortcutIdentifier.Add.type:
      // Handle shortcut 2 (static).
      handled = true
    break
    default:
    break
  }

  // Construct an alert using the details of the shortcut used to open the application.
  let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert)
  let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
  alertController.addAction(okAction)
  
  // Display an alert indicating the shortcut selected from the home screen.
  window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
 
  return handled
}

And finally we need call this method in the next AppDelegate situations:

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) {
  let handledShortCutItem = handleShortCutItem(shortcutItem)
  completionHandler(handledShortCutItem)
}

And in applicationDidBecomeActive:

func applicationDidBecomeActive(application: UIApplication) {
  guard let shortcut = launchedShortcutItem else { return }
  handleShortCutItem(shortcut)
  launchedShortcutItem = nil
}

That’s all. How to work with dynamic items, you can check an official Apple example, there is not present any difficulties.

NOTE: If you don’t have real iPhone 6 or iPhone 6 Plus, you can test on a simulator with helping this.

About

iOS 3D Touch: How to add quick actions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages