Short is a tiny wrapper that helps you to easily add Siri Shortcuts to your app with few lines of code.
- iOS 12
- Xcode 10
To install Short using Carthage, add the following line to your Cartfile:
github "TheAbstractDev/Short"
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 !
import Short
Short.getAllVoiceShortcuts { shortcuts in
for shortcut in shortcuts {
print(shortcut)
}
}
let customIntent = CustomIntent()
Short.getVoiceShortcutFrom(intent: customIntent)
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)
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
}
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)
}
import IntentsUI
To get and receive events from the SiriVoiceShortcutViewControllers, you have to conform your ViewController to the required delegates.
extension ViewController: INUIAddVoiceShortcutViewControllerDelegate {
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) {
// add shortcut
dismiss(animated: true)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
dismiss(animated: true)
}
}
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)
}
}
You can test the Example project by downloading and running it.
Short is available under the MIT license. See the LICENSE file for more info.