Swift wrapper for iOS Home Screen Quick Actions (App Icon Shortcuts)
Switch branches/tags
Nothing to show
Clone or download
Latest commit f08d986 Jul 23, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Example Xcode 8: Swift 3.0 (#8) Nov 30, 2016
QuickActions.xcodeproj Bump v2.0.0 Nov 30, 2016
QuickActions Bump v2.0.0 Nov 30, 2016
Source Xcode 8: Swift 3.0 (#8) Nov 30, 2016
fastlane Bump v2.0.0 Nov 30, 2016
.gitignore Xcode 8: Swift 3.0 (#8) Nov 30, 2016
.swift-version Xcode 8: Swift 3.0 (#8) Nov 30, 2016
LICENSE First release Feb 24, 2016
QuickActions.podspec Bump v2.0.0 Nov 30, 2016
README.md Update README.md Jul 22, 2017

README.md

Carthage Compatible CocoaPods Compatible Swift 3.0 Platforms iOS License MIT

QuickActions

Swift wrapper for iOS Home Screen Quick Actions

This wrapper creates dynamic quick actions. It is possible to define static quick actions in your app’s Info.plist file but I think it is nicer to add localizable shortcuts dynamically and handle them with type safety.

Usage

import QuickActions

Define your application shortcuts with an enum. Don't forget to declare the enum with String and ShortcutType:

enum AppShortcut: String, ShortcutType {
    case createExpense
    case lastItems
}

Install a list of shortcuts:

var quickActions: QuickActions<AppShortcut>?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? = nil) -> Bool {
    let shortcuts = [Shortcut(type: AppShortcut.CreateExpense, title: NSLocalizedString("CreateExpenseTitle", comment: ""), subtitle: NSLocalizedString("CreateExpenseSubTitle", comment: ""), icon: .Add)]

    if let actionHandler = window?.rootViewController as? QuickActionSupport, bundleIdentifier = NSBundle.mainBundle().bundleIdentifier {
        quickActions = QuickActions(application, actionHandler: actionHandler, bundleIdentifier: bundleIdentifier, shortcuts: shortcuts, launchOptions: launchOptions)
    }
}

Add more shortcuts:

func applicationDidEnterBackground(application: UIApplication) {
    let shortcuts = [Shortcut(type: AppShortcut.lastItems, title: "Last items", subtitle: nil, icon: nil)]
    quickActions?.add(shortcuts, toApplication: application)
}

Handle each shortcut:

@available(iOS 9, *)
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Swift.Void) {
    // This callback is used if your application is already launched in the background, if not application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) will be called (handle the shortcut in those callbacks and return `false`)
    guard let quickActions = quickActions else {
        return completionHandler(false)
    }
    guard let actionHandler = window?.rootViewController as? QuickActionSupport else {
        return completionHandler(false)
    }
    completionHandler(quickActions.handle(actionHandler, shortcutItem: shortcutItem))
}

Prepare your view controller using the QuickActionSupport protocol:

class MainViewController: UIViewController, QuickActionSupport {

    func prepareForQuickAction<T: ShortcutType>(_ shortcutType: T) {
        if let shortcut = AppShortcut(rawValue: shortcutType.value), case .createExpense = shortcut {
            print("Prepare the view to create a new expense")
        }

        //or

        if let shortcut = AppShortcut(rawValue: shortcutType.value) {
            switch shortcut {
            case .createExpense:
                print("Prepare the view to create a new expense")
            case .lastItems:
                print("Prepare the view to show last items")
            }
        }
    }    

}

Installation

Carthage

To install it, simply add the following line to your Cartfile:

github "ricardopereira/QuickActions" ~> 2.0.0

Then run carthage update.

Follow the current instructions in Carthage's README for up to date installation instructions.

CocoaPods

To install it, simply add the following line to your Podfile:

pod 'QuickActions' '~> 2.0.0'

You will also need to make sure you're opting into using frameworks:

use_frameworks!

Then run pod install with CocoaPods 1.1.0 or newer.

Manually

  1. Download and drop QuickActions.swift in your project.
  2. Congratulations!

Requirements

  • iOS 9.0+
  • Xcode 8 (Swift 3.0)

Author

Ricardo Pereira, @ricardopereiraw

License

QuickActions is available under the MIT license. See the LICENSE file for more info.