Using Application Shortcuts

      No Comments on Using Application Shortcuts

With iPhone 6s came the 3D touch, and with it we got a few more options to interact with the users. In this short blog post we’ll examine one of those options called ‘Application Shortcuts’, or sometimes called ‘Home Screen Quick Actions’, we’ll call them application shortcuts, because it’s shorter.

In a few words, application shortcuts is a menu that is available to you when you force touch on an application icon. If you have an iPhone 6s (or 6s plus), press really hard on an app like Facebook, and you’ll see what I mean, make sure you have force touch enabled, or you might break the screen trying to display the menu 🙂 ).

Example

Implementing application shortcuts is really simple, and we’ll go through a basic example of dynamic application shortcuts that you will be able to use in your apps and expand on it as you see fit.

You can create up to four menu items, and each item is of type UIApplicationShortcutItem. All we have to do is create an array of our shortcut items, and set it on our shared application.

First lets define an enum for our menu items, so to make it extensible, like so:

enum ApplicationShortcutItem: String {
    
    case Item1
    case Item2
    case Item3
    case Item4
    
    var title: String {
        get {
            switch self {
            case .Item1: return "Item Title 1"
            case .Item2: return "Item Title 2"
            case .Item3: return "Item Title 3"
            case .Item4: return "Item Title 4"
            }
        }
    }
    
    var subtitle: String? {
        get {
            switch self {
            case .Item1: return "Item 1 subtitle"
            case .Item3: return "Item 3 subtitle"
            default: return nil
            }
        }
    }
    
    var icon: UIApplicationShortcutIcon? {
        get {
            return nil
        }
    }
}

With our enum in place we can create an array of menu items:

func applicationShortcuts() -> [UIApplicationShortcutItem] {
        return [ApplicationShortcutItem.Item1,
                ApplicationShortcutItem.Item2,
                ApplicationShortcutItem.Item3,
                ApplicationShortcutItem.Item4]
            .map { UIApplicationShortcutItem(type: $0.rawValue, localizedTitle: $0.title, localizedSubtitle: $0.subtitle, icon: $0.icon) }
    }

Now we have an array of shortcut items, and we can set it on our shared application:

UIApplication.shared.shortcutItems = self.shortcutManager.applicationShortcuts()

If you build and run your app on a device you can see the menu by force touching on the app icon. As shown on the next image:

All that’s left is hookup the delegate callback and process the selected item. We’ll implement a delegate callback in the app delegate:

public func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        
        self.displayAlert(withShortcut: shortcutItem)
        
        self.shortcutManager.processShortcutItem(shortcutItem: shortcutItem)
        
        completionHandler(true)
    }

In my implementation you will se an alert popping up to give you some visual feedback that everything works, obviously you won’t need this in your implementation. I’m using a shortcut manager to generate and process menu items, we’ll see the ‘process shortcut item’ implementation in a moment. At the end of the function you should call the completion handler with a boolean value, true if your action succeeded and false if it failed.

In the shortcut manager we have a function that will process selected menu items:

func processShortcutItem(shortcutItem: UIApplicationShortcutItem) {
        
        switch shortcutItem.type {
        case ApplicationShortcutItem.Item1.rawValue:
            print("Item 1 custom action")
        case ApplicationShortcutItem.Item2.rawValue:
            print("Item 2 custom action")
        case ApplicationShortcutItem.Item3.rawValue:
            print("Item 3 custom action")
        case ApplicationShortcutItem.Item4.rawValue:
            print("Item 4 custom action")
        default:
            print("Unknown item selected")
        }
    }

In this function you would perform your custom actions, of course, this depends on the nature of your application, some of the actions you could perform might be displaying a view controller, opening another app, opening a url, whatever comes to your mind.

This has been a short and fun post, I hope you’ll find something useful in it, you can find the example project on my GitHub account, feel free to use it.

As always, have a nice day 🙂

Dejan.

More resources

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.