QuickActions 7.0.0

QuickActions 7.0.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最新发布2023年10月
SPM支持 SPM

Ricardo Pereira 维护。



Carthage Compatible CocoaPods Compatible Swift 5.0 Platforms iOS License MIT

QuickActions

Swift 封装用于 iOS 主屏幕快捷操作

此封装创建了动态快捷操作。您可以在应用的 Info.plist 文件中定义静态快捷操作,但我认为动态添加可本地化的快捷方式并使用类型安全处理它们会更方便。

用法

import QuickActions

使用枚举定义您的应用程序快捷方式。不要忘记用 StringShortcutType 声明枚举。

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

安装快捷方式列表

var quickActions: QuickActions<AppShortcut>?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: 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 = Bundle.main.bundleIdentifier {
        quickActions = QuickActions(application, actionHandler: actionHandler, bundleIdentifier: bundleIdentifier, shortcuts: shortcuts, launchOptions: launchOptions)
    }
}

添加更多快捷方式

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

处理每个快捷方式

@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))
}

使用 QuickActionSupport 协议准备您的视图控制器

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")
            }
        }
    }    

}

安装

Carthage

要安装它,只需将以下行添加到您的 Cartfile

github "ricardopereira/QuickActions" ~> 5.0.0

然后运行 carthage update

有关最新安装说明,请遵循Carthage的README中的当前说明。

CocoaPods

要安装它,只需将以下行添加到您的 Podfile

pod 'QuickActions' '~> 5.0.0'

您还需要确保您已经选择使用框架

use_frameworks!

然后使用CocoaPods 1.6.0或更高版本运行 pod install

手动

  1. 下载并将 QuickActions.swift 拖放到您的项目中。
  2. 恭喜!

需求

  • iOS 8.1+
  • Xcode 10.2 (Swift 5.0)

作者

Ricardo Pereira, @ricardopereiraw

许可证

QuickActions 可以在 MIT 许可下使用。更多信息请查看 LICENSE 文件。