Git Product home page Git Product logo

quickactions's Introduction

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

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: [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)
    }
}

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

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

pod 'QuickActions' '~> 6.0.0'

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

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, @ricardopereiraw

License

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

quickactions's People

Contributors

lfarah avatar moathothman avatar ricardopereira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

quickactions's Issues

Implement custom icon

I'm currently implementing QuickActions with your framework. I want to set custom icons for the quick actions but I don't really know how exactly. In your source there's the following line:

if self == .custom {
            NSException(name: NSExceptionName(rawValue: "Invalid option"), reason: "`Custom` type need to be used with `toApplicationShortcutIcon:imageName`", userInfo: nil).raise()
            return nil
        }

To build the shortcuts my code looks like in your example:

let shortcuts = [
            Shortcut(
                type: AppShortcut.createExpense,
                title: NSLocalizedString("CreateExpenseTitle", comment: ""),
                subtitle: NSLocalizedString("CreateExpenseSubTitle", comment: ""),
                icon: .add
            )
        ]

There's this function:

func toApplicationShortcutIcon(_ imageName: String) -> UIApplicationShortcutIcon?

But I don't really know how to call this function or is there another way? Can someone give me an advice how to implement a custom icon?

Swift 3 compatibility

Hey, I'd love to see this lib available in Swift 3 so I can implement it in my project.

Thanks ๐Ÿ˜„

Installation

Hello Ricardo,

Thanks for this module . I was looking forward an easier solution of implementing Quick Actions with my app. However, I ran into some installation issues. Module is installed correctly and recognised with "import" statement at the beginning of my AppDelegate, but then there are some problems:

Use of undeclared type 'ShortcutType'
Cannot specialize non-generic type 'module<QuickActions>'

Truth be told I'm just following the steps you outlined in the readme file. At this point I'm just guessing which part goes where in my app. Could you please provide and example application for everyone to use as kickstart ?

Manual Installation

CocoaPods and Carthage are awesome tools and make our life really easier, but there are some devs who still don't know how to use them.

It would be cool to add the Manual installation guide in your README.md. You can take a look at my iOS Readme Template to see how you can do it.

Support for Swift 4.2 and newer

The new Xcode version force me to update my project to a newer Swift version than 4.0. Now this pod is outdated. I'd really appreciate an update :).

Somehow not working to me. Anyone works?

I ran into one problem and couldn't figure it out what happened. I followed the instructions in the README and also checked the example code, however in performActionForShortcutItem function

func application(application: UIApplication, performActionForShortcutItem shortcutItem:     UIApplicationShortcutItem, completionHandler: Bool -> Void) {
        guard let quickActions = quickActions else { return completionHandler(false) }
        guard let rootViewController = window?.rootViewController else { return completionHandler(false) }
        completionHandler(quickActions.handle(rootViewController, shortcutItem: shortcutItem))
}

This line always return false

quickActions.handle(rootViewController, shortcutItem: shortcutItem)

I don't know if it is because I set something wrong or it is because of the library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.