项目作者: ricardopereira

项目描述 :
Swift wrapper for iOS Home Screen Quick Actions (App Icon Shortcuts)
高级语言: Swift
项目地址: git://github.com/ricardopereira/QuickActions.git
创建时间: 2016-02-24T00:24:09Z
项目社区:https://github.com/ricardopereira/QuickActions

开源协议:MIT License

下载


CocoaPods Compatible
Swift 5.0
Platforms iOS
License MIT

QuickActions

Swift wrapper for iOS Home Screen Quick Actions

This wrapper helps you create dynamic Quick Actions. It is possible to define static quick actions in your app’s Info.plist file and also add localizable shortcuts dynamically and handle them with type safety.

Usage

  1. import QuickActions

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

  1. enum AppShortcut: String, ShortcutType {
  2. case createExpense
  3. case lastItems
  4. }

Install a list of shortcuts:

  1. var quickActions: QuickActions<AppShortcut>?
  2. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
  3. let shortcuts = [Shortcut(type: AppShortcut.CreateExpense, title: NSLocalizedString("CreateExpenseTitle", comment: ""), subtitle: NSLocalizedString("CreateExpenseSubTitle", comment: ""), icon: .Add)]
  4. if let actionHandler = window?.rootViewController as? QuickActionSupport, bundleIdentifier = Bundle.main.bundleIdentifier {
  5. quickActions = QuickActions(application, actionHandler: actionHandler, bundleIdentifier: bundleIdentifier, shortcuts: shortcuts, launchOptions: launchOptions)
  6. }
  7. }

Add more shortcuts:

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

Handle each shortcut:

  1. @available(iOS 9, *)
  2. func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Swift.Void) {
  3. // 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`)
  4. guard let quickActions = quickActions else {
  5. return completionHandler(false)
  6. }
  7. guard let actionHandler = window?.rootViewController as? QuickActionSupport else {
  8. return completionHandler(false)
  9. }
  10. completionHandler(quickActions.handle(actionHandler, shortcutItem: shortcutItem))
  11. }

Prepare your view controller using the QuickActionSupport protocol:

  1. class MainViewController: UIViewController, QuickActionSupport {
  2. func prepareForQuickAction<T: ShortcutType>(_ shortcutType: T) {
  3. if let shortcut = AppShortcut(rawValue: shortcutType.value), case .createExpense = shortcut {
  4. print("Prepare the view to create a new expense")
  5. }
  6. //or
  7. if let shortcut = AppShortcut(rawValue: shortcutType.value) {
  8. switch shortcut {
  9. case .createExpense:
  10. print("Prepare the view to create a new expense")
  11. case .lastItems:
  12. print("Prepare the view to show last items")
  13. }
  14. }
  15. }
  16. }

Installation

CocoaPods" class="reference-link"> CocoaPods

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

  1. pod 'QuickActions' '~> 6.0.0'

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

  1. use_frameworks!

Then run pod install with CocoaPods 1.8.0 or newer.

Manually

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

Requirements

  • iOS 12+
  • Xcode 13+ (Swift 5)

Author

Ricardo Pereira, @ricardopereira"">@ricardopereiraw

License

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