Fork me on GitHub

Alex Rupérez

Link: http://j.mp/2znjUjq
Published: 09 Jul 13:44
I starred TheAbstractDev/Short at GitHub.
Easily add Siri Shortcuts to your App.
View on GitHub


View README


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.