Skip to content

Instantly share code, notes, and snippets.

@shiningabdul
Last active March 2, 2020 06:07
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/ad56baa1485b7a012eecfb1b9729a5ca to your computer and use it in GitHub Desktop.
Save shiningabdul/ad56baa1485b7a012eecfb1b9729a5ca to your computer and use it in GitHub Desktop.
IntentHandler extension for handling SiriKit requests
import Intents
class IntentHandler: INExtension {
override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.
if intent is OrderPizzaIntent {
return OrderPizzaIntentHandler()
}
return self
}
}
import Foundation
import Intents
class OrderPizzaIntentHandler: NSObject, OrderPizzaIntentHandling {
func confirm(intent: OrderPizzaIntent, completion: @escaping (OrderPizzaIntentResponse) -> Void) {
completion(OrderPizzaIntentResponse(code: .ready, userActivity: nil))
}
func handle(intent: OrderPizzaIntent, completion: @escaping (OrderPizzaIntentResponse) -> Void) {
guard intent.topping != nil else {
completion(OrderPizzaIntentResponse(code: .failure, userActivity: nil))
return
}
let amount = INCurrencyAmount(amount: NSDecimalNumber(decimal: 23.14), currencyCode: "CAD")
completion(.success(total: amount))
}
func resolveTopping(for intent: OrderPizzaIntent, with completion: @escaping (OrderPizzaToppingResolutionResult) -> Void) {
guard let topping = intent.topping, !topping.isEmpty else {
completion(.needsValue())
return
}
guard topping.lowercased() == "pepperoni" else {
completion(.unsupported(forReason: .ingrediantNotAvailable))
return
}
completion(.success(with: topping))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment