Skip to content

Instantly share code, notes, and snippets.

@shiningabdul
Last active March 1, 2020 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiningabdul/db58ab236265ea75959afd3e1b14fba7 to your computer and use it in GitHub Desktop.
Save shiningabdul/db58ab236265ea75959afd3e1b14fba7 to your computer and use it in GitHub Desktop.
ViewController that loads an INUIAddVoiceShortcutButton.
import UIKit
import IntentsUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let addShortcutButton = INUIAddVoiceShortcutButton(style: .blackOutline)
let intent = OrderPizzaIntent()
intent.suggestedInvocationPhrase = "Order pizza"
addShortcutButton.shortcut = INShortcut(intent: intent)
addShortcutButton.delegate = self
addShortcutButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(addShortcutButton)
view.centerYAnchor.constraint(equalTo: addShortcutButton.centerYAnchor).isActive = true
view.centerXAnchor.constraint(equalTo: addShortcutButton.centerXAnchor).isActive = true
}
}
extension ViewController : INUIAddVoiceShortcutButtonDelegate {
func present(_ addVoiceShortcutViewController: INUIAddVoiceShortcutViewController, for addVoiceShortcutButton: INUIAddVoiceShortcutButton) {
addVoiceShortcutViewController.delegate = self
present(addVoiceShortcutViewController, animated: true, completion: nil)
}
func present(_ editVoiceShortcutViewController: INUIEditVoiceShortcutViewController, for addVoiceShortcutButton: INUIAddVoiceShortcutButton) {
editVoiceShortcutViewController.delegate = self
present(editVoiceShortcutViewController, animated: true, completion: nil)
}
}
extension ViewController : INUIAddVoiceShortcutViewControllerDelegate {
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController,
didFinishWith voiceShortcut: INVoiceShortcut?,
error: Error?) {
if let error = error as NSError? {
print("Error adding voice shortuct: %@", error)
}
controller.dismiss(animated: true, completion: nil)
}
func addVoiceShortcutViewControllerDidCancel(_ controller: INUIAddVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
extension ViewController : INUIEditVoiceShortcutViewControllerDelegate {
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController,
didUpdate voiceShortcut: INVoiceShortcut?,
error: Error?) {
if let error = error as NSError? {
print("Error adding voice shortcut: %@", error)
}
controller.dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewController(_ controller: INUIEditVoiceShortcutViewController,
didDeleteVoiceShortcutWithIdentifier deletedVoiceShortcutIdentifier: UUID) {
controller.dismiss(animated: true, completion: nil)
}
func editVoiceShortcutViewControllerDidCancel(_ controller: INUIEditVoiceShortcutViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment