我正在构建一个使用Intents Extension来跟踪各种指标的应用程序,例如体重,步数,炉膛速率等。我想根据他想要跟踪的指标向用户提供可供选择的测量单位。例如,如果用户跟踪饮用水,她可以在升,液量盎司,毫升或杯子之间进行选择,我将向她展示单位。如果用户跟踪步骤,则使用的唯一单位是“步骤”,因此没有必要为其提供歧义或动态选项。我注意到,使用动态选项时,即使在resolve函数中发送notRequired解析结果时,也会每次调用为其生成的provideUnitOptions函数。
这是我的意图处理程序中的函数:
func resolveMetric(for intent: MeasurementIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
let requestedMetric = intent.metric
if requestedMetric == nil {
completion(INStringResolutionResult.needsValue())
} else {
metricService.retrieveUserMetrics { (success) in
let metricNames = self.metricService.metricNamesMap.keys
if metricNames.contains(requestedMetric!){
completion(.success(with: requestedMetric!))
} else {
completion(.disambiguation(with: Array(metricNames)))
}
}
}
}
func provideMetricOptions(for intent: MeasurementIntent, with completion: @escaping ([String]?, Error?) -> Void) {
metricService.retrieveUserMetrics { (success) in
let metricNames = self.metricService.metricNamesMap.keys
completion(Array(metricNames), nil)
}
}
func resolveUnit(for intent: MeasurementIntent, with completion: @escaping (INStringResolutionResult) -> Void) {
let metric = self.metricService.metricNamesMap[intent.metric!]!
let units = metric.units.getSystemUnits(defaultUnit: metric.defaultUnit).map { $0.name }
if units.count == 1 {
completion(.notRequired())
} else if intent.unit == nil {
completion(.disambiguation(with: units))
} else {
completion(.success(with: intent.unit!))
}
}
//This is called even if in resolveUnit I sent .notRequired resolution. Same for .success
func provideUnitOptions(for intent: MeasurementIntent, with completion: @escaping ([String]?, Error?) -> Void) {
let metric = self.metricService.metricNamesMap[intent.metric!]!
let units = metric.units.getSystemUnits(defaultUnit: metric.defaultUnit).map { $0.name }
completion(units, nil)
}
resolveUnit函数在provideUnitOptions之前和之后均被调用我可以禁用动态选项,但在这种情况下,用户将无法使用快捷方式中的预定义单位。您认为我应该如何进行?我想念什么吗?谢谢!
找到了可以帮助我克服这个问题的工作。因此,将至少在第一次为诸如“ success”和“ notRequired”之类的解决方案完成而调用provideOptions,但不会因消除歧义或确认Required而被调用。消除歧义在这里无济于事,但是如果我使用confirmationRequired,则可以自动通过我的唯一选择。在确认提示中,我不必包括字段值,任何有效的方法。
if units.count == 1 {
completion(.confirmationRequired(with: units[0]))
}
希望在以后的iOS更新中将有一种更加动态的方式来处理用户参数,但这已经足够用了