Last active
March 2, 2020 06:07
-
-
Save shiningabdul/ad56baa1485b7a012eecfb1b9729a5ca to your computer and use it in GitHub Desktop.
IntentHandler extension for handling SiriKit requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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