3D Touch Introduction : Static Quick Home Screen Actions

iOS 9 introduces 3D touch using which user can hard press on home screen icon and get shortcuts to some actions in the application, or user can hard-press on controls in the application to get more options for that control or see previews of other contents.

We have integrated 3D touch in our client’s applications and also in our in-house applications. To share an example, let me show you an example of our Blood Monk application, in which we have integrated 3D Touch for the quick home screen actions.

IMG_1714

 

Now follow the below steps to integrate 3D touch for the quick home screen actions.

Add home screen actions to your app

  1. Right click on your info.plist file. open As / Source code.
  2. Copy below keys of info.plist and paste it in your projects info.plist file.

     <key>UIApplicationShortcutItems</key>
    	<array>
    		<dict>
    			<key>UIApplicationShortcutItemIconFile</key>
    			<string>ic_learn1_traffic_e</string>
    			<key>UIApplicationShortcutItemSubtitle</key>
    			<string></string>
    			<key>UIApplicationShortcutItemTitle</key>
    			<string>Learn</string>
    			<key>UIApplicationShortcutItemType</key>
    			<string>$(PRODUCT_BUNDLE_IDENTIFIER).Learn</string>
    		</dict>
    		<dict>
    			<key>UIApplicationShortcutItemIconFile</key>
    			<string>driving_kanun_e</string>
    			<key>UIApplicationShortcutItemSubtitle</key>
    			<string></string>
    			<key>UIApplicationShortcutItemTitle</key>
    			<string>Driving Laws</string>
    			<key>UIApplicationShortcutItemType</key>
    			<string>$(PRODUCT_BUNDLE_IDENTIFIER).DrivingLaws</string>
    		</dict>
    		<dict>
    			<key>UIApplicationShortcutItemIconFile</key>
    			<string>ic_test1_traffic_e</string>
    			<key>UIApplicationShortcutItemSubtitle</key>
    			<string></string>
    			<key>UIApplicationShortcutItemTitle</key>
    			<string>Test</string>
    			<key>UIApplicationShortcutItemType</key>
    			<string>$(PRODUCT_BUNDLE_IDENTIFIER).Test</string>
    		</dict>
    	</array>

    Now again right click ono info.plist file open as/property list. you will see something like below.

    Screen Shot 2016-08-11 at 12.40.45 PM
     UIApplicationShortcutItemIconFile

    using this option you can change your shortcut’s icon.

    UIApplicationShortcutItemTitle

    using this option you can change your shortcut’s title.

    UIApplicationShortcutItemSubtitle

    using this option you can change your shortcut’s subtitle

    UIApplicationShortcutItemType

    using this option you can identify uniquely your shortcut.

    Change these 4 keys according to your need. you can add more shortcuts or remove some also from plist as per your need.

  3. Now open Appdelegate.swift file. add below code.
    after class declaration. These are types of shortcut items (unique identifiers)
     enum Shortcut: String {
            case Learn = "Learn"
            case DrivingLaws = "DrivingLaws"
            case Test = "Test"
            
        }
    
  4. Now add below method. When user touches any shortcut item this method will be called and you will get all the information about shortcut item from object shortcutitem as below.
     func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
            
            // Handle quick actions
            completionHandler(handleQuickAction(shortcutItem))
                         //return handled
            
        }
     @available(iOS 9.0, *)
        func handleQuickAction(shortcutItem: UIApplicationShortcutItem) -> Bool {
                  var quickActionHandled = false
            let type = shortcutItem.type.componentsSeparatedByString(".").last!
            if let shortcutType = Shortcut.init(rawValue: type) {
                switch shortcutType {
                case .Learn:
                    quickActionHandled = true
                    self.LearnTouch()
                    break
                case .DrivingLaws:
                    quickActionHandled = true
                    self.DrivingLawsTouch()
                    break
                case .Test:
                    quickActionHandled = true
                    self.TestTouch()
                    break
                    
                }
            }
            return quickActionHandled
        }
    
  5. Now add this method.
     func LearnTouch()
        {
            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let controller : CustomNavigation =      storyBoard.instantiateViewControllerWithIdentifier("CustomNavigation") as! CustomNavigation
            let mainviewcontroller : HomeVC = storyBoard.instantiateViewControllerWithIdentifier("HomeVC") as! HomeVC
            let viewcontroller : LearnVC = storyBoard.instantiateViewControllerWithIdentifier("LearnVC") as! LearnVC
            controller.viewControllers=[mainviewcontroller,viewcontroller]
            self.window?.rootViewController = controller
    
        }

    You can make changes to these methods according to your navigation requirement.
  6. That’s it for quick home screen actions. Enjoy coding.

Want to work with us? We're hiring!