Giter Site home page Giter Site logo

short's Introduction

Short

Carthage compatible Platform License

Short is a tiny wrapper that helps you to easily add Siri Shortcuts to your app with few lines of code.

Prerequisites

  • iOS 12
  • Xcode 10

Installation

Carthage

To install Short using Carthage, add the following line to your Cartfile:

github "TheAbstractDev/Short"

Creating Custom Intents

Before using Short, you'll have to create a new SiriKit Intent Definition File.

Once the file is created, click on the + sign and select New Intent and add the associated properties.

Your intent must be associated with a Class !

Usage

import Short

Getting All Voice Shortcuts

Short.getAllVoiceShortcuts { shortcuts in
    for shortcut in shortcuts {
        print(shortcut)
    }
}

Getting a Voice Shortcut from an Intent

let customIntent = CustomIntent()
Short.getVoiceShortcutFrom(intent: customIntent) 

Donate Intent Interations.

Before Siri can suggest shortcuts to the user, the app must tell Siri about the shortcuts through intent donations. An app makes a donation each time the user performs an action in the app.

let customIntent = CustomIntent()
Short.donateInteraction(for: customIntent)

Handle User Activity.

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if let orderIntent = userActivity.interaction?.intent as? CustomIntent {
        var response = CustomIntentResponse()
        if orderIntent.quantity as! Int > 0 {
            response = CustomIntentResponse(code: .success, userActivity: nil)
        } else {
            response = CustomIntentResponse(code: .failure, userActivity: nil)
        }
        
        switch response.code {
        case .success:
	    // Do some stuff
            return true
        case .failure:
	    // Do some stuff
            return false
	default:
            break
        }
    }
    return false
}

Adding Phrases to Siri.

let customIntent = CustomIntent()
let shortcut = Short.getShortcutFrom(intent: customIntent)
if let shortcut = shortcut {
    Short.presentSiriShortcutViewController(from: self, shortcut: shortcut)
}

If a phrase already exists for the shortcut, the user can also change the phrase directly from your app.

let customIntent = CustomIntent()
let shortcut = Short.getShortcutFrom(intent: customIntent)
if let shortcut = shortcut {
   Short.presentExistingSiriShortcutViewController(existingShortcut: shortcut)
}

Delegates methods

import IntentsUI

To get and receive events from the SiriVoiceShortcutViewControllers, you have to conform your ViewController to the required delegates.

INUIAddVoiceShortcutViewControllerDelegate

extension ViewController: INUIAddVoiceShortcutViewControllerDelegate {
    func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
        // add shortcut
        dismiss(animated: true)
    }
    
    func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
        dismiss(animated: true)
    }    
}

INUIEditVoiceShortcutViewControllerDelegate

extension ViewController: INUIEditVoiceShortcutViewControllerDelegate {
    func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didUpdate voiceShortcut: INVoiceShortcut?, error: Error?) {
        // update shortcut
        dismiss(animated: true)
    }
    
    func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController, didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
        // delete shortcut
        dismiss(animated: true)
    }
    
    func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
        dismiss(animated: true)
    }
}

Example

You can test the Example project by downloading and running it.

License

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

short's People

Contributors

theabstractdev avatar

Watchers

 avatar  avatar

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.