Siri ShortCut을 위한 샘플앱
SiriKit (iOS 10이상) Intents, Intents UI Framework
User(request) -> SiriKit -> APP Task
우리의 앱과 시스템의 커뮤니케이션을 도와주는 프레임워크
Intents를 UI로 커스텀할수있게 만들어줌
import UIKit
import WeatherKit
import Intents
class CityWeatherIntentHandler: NSObject, CityWeatherIntentHandling {
func resolveCity(for intent: CityWeatherIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
guard let city = intent.city else {
return
}
completion(.success(with: city ))
}
func confirm(intent: CityWeatherIntent, completion: @escaping (CityWeatherIntentResponse) -> Void) {
completion(CityWeatherIntentResponse(code: .ready, userActivity: nil))
}
func handle(intent: CityWeatherIntent, completion: @escaping (CityWeatherIntentResponse) -> Void) {
let weatherManager = WeatherManager()
let cities = weatherManager.cities
guard let city = intent.city, cities.contains(city) else {
completion(CityWeatherIntentResponse.failureNoCity(intent.city ?? "City"))
return
}
weatherManager.getWeather(at: city) { weatherInfo in
completion(CityWeatherIntentResponse.success(city: city, weather: weatherInfo.briefWeather))
}
}
}
7) 이 후 Appdelegate에 Siri를 통해 들어온 의도를 처리할수 있는 userActivity 내용을 구현함
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
// get the city name
guard let intent = userActivity.interaction?.intent as? CityWeatherIntent,
let city = intent.city else {
return false
}
// get the dsired view
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WeatherViewController") as! WeatherViewController
vc.cityName = city
// present the desired view
let rootViewController = window?.rootViewController as? UINavigationController
rootViewController?.pushViewController(vc, animated: true)
return true
}
**참고 URL
https://itnext.io/siri-shortcut-tutorial-using-custom-intent-d0f836af5863