Wednesday, May 25, 2016

Add 3D Touch to App Icon

Note: As of this writing the Simulator does not support 3D Touch.

Step 1: Define Shortcut Menu Items

Do this in the Info.plist

I defined the icon type (UIApplicationShortcutItemTypeShare) to show the share icon. There are 29 different icons you can choose from as of this writing. Find them here.

Step 2: Handle Action for Shortcut Item

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
    if let tabVC = self.window?.rootViewController as? UITabBarController {
        if shortcutItem.type == "Search" {
            tabVC.selectedIndex = 0
        } else if shortcutItem.type == "Add New" {
            tabVC.selectedIndex = 1
        }
    }
}

Want to create a dynamic shortcut?

Maybe you want to show/not show shortcuts or show shortcuts with dynamic info like a count of items.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // DYNAMIC SHORTCUT
    // Download new data to add it in the localizedSubtitle.
    // Like localizedSubtitle: "You have \(number) new ____"
    if let shortcutItems = application.shortcutItems where shortcutItems.isEmpty {
        let dynamicShortcut =
            UIMutableApplicationShortcutItem(type: "Add New",
                                             localizedTitle: "Add New",
                                             localizedSubtitle: "Create a new lead",
                                             icon: UIApplicationShortcutIcon(templateImageName: "addNewIcon"),
                                             userInfo: nil)
        application.shortcutItems = [dynamicShortcut]
    }
    return true
}

(Swift 2.2)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...