在我的应用程序中,我只支持欧元和美元货币。因此,当用户尝试使用Siri发送英镑等货币的付款时,我会要求他在欧元和美元之间进行选择。
之后,我在屏幕上看到:
- 100美元 - 100欧元
如果我在`intent.currencyAmount!.currencyCode`中选择了100美元,我总是得到英镑(但用户选择的是美元)。这非常奇怪。
这是我的代码:
1)创建一个新的意图扩展。
2)在plist文件中只留下一种意图类型:http://take.ms/pt16N 3)您的IntentHandler类(由xCode创建)必须确认符合INSendPaymentIntentHandling协议。
4)在IntentHandler类中添加以下内容:
之后,我在屏幕上看到:
- 100美元 - 100欧元
如果我在`intent.currencyAmount!.currencyCode`中选择了100美元,我总是得到英镑(但用户选择的是美元)。这非常奇怪。
这是我的代码:
func resolveCurrencyAmount(forSendPayment intent: INSendPaymentIntent, with completion: @escaping (INCurrencyAmountResolutionResult) -> Void) {
if let currencyAmount = intent.currencyAmount { // need to check if we have proper value
if let amount = currencyAmount.amount {
if amount.doubleValue == 0 { // wrong amount
completion(INCurrencyAmountResolutionResult.unsupported())
return
}
if let currencyCode = currencyAmount.currencyCode {
if let _ = EnumCurrency(rawValue: currencyCode) { // we found currency code that we know
completion(INCurrencyAmountResolutionResult.success(with: INCurrencyAmount(amount: NSDecimalNumber(value: abs(amount.doubleValue)), currencyCode: currencyCode)))
return
}
}
// if we are here so we don't have proper currency, try to offer user to choose the same amount but with all possible currencies
let disambiguationArray: [INCurrencyAmount] = EnumCurrency.allValues.map({ (currency) -> INCurrencyAmount in
return INCurrencyAmount(amount: NSDecimalNumber(value: abs(amount.doubleValue)), currencyCode: currency.rawValue)
})
completion(INCurrencyAmountResolutionResult.disambiguation(with: disambiguationArray))
}
}
else { // we don't have value
completion(INCurrencyAmountResolutionResult.needsValue())
}
}
enum EnumCurrency : String {
case EUR = "EUR"
case USD = "USD"
static let allValues = [EUR, USD]
}
更新:如何重现(根据David的问题):1)创建一个新的意图扩展。
2)在plist文件中只留下一种意图类型:http://take.ms/pt16N 3)您的IntentHandler类(由xCode创建)必须确认符合INSendPaymentIntentHandling协议。
4)在IntentHandler类中添加以下内容:
func resolveCurrencyAmount(forSendPayment intent: INSendPaymentIntent, with completion: @escaping (INCurrencyAmountResolutionResult) -> Void) {
if let currencyAmount = intent.currencyAmount { // need to check if we have proper value
if let amount = currencyAmount.amount {
if amount.doubleValue == 0 { // wrong amount
completion(INCurrencyAmountResolutionResult.unsupported())
return
}
if let currencyCode = currencyAmount.currencyCode {
if let _ = EnumCurrency(rawValue: currencyCode) { // we found currency code that we know
completion(INCurrencyAmountResolutionResult.success(with: INCurrencyAmount(amount: NSDecimalNumber(value: abs(amount.doubleValue)), currencyCode: currencyCode)))
return
}
}
// if we are here so we don't have proper currency, try to offer user to choose the same amount but with all possible currencies
let disambiguationArray: [INCurrencyAmount] = EnumCurrency.allValues.map({ (currency) -> INCurrencyAmount in
return INCurrencyAmount(amount: NSDecimalNumber(value: abs(amount.doubleValue)), currencyCode: currency.rawValue)
})
completion(INCurrencyAmountResolutionResult.disambiguation(with: disambiguationArray))
}
}
else { // we don't have value
completion(INCurrencyAmountResolutionResult.needsValue())
}
}
enum EnumCurrency : String {
case EUR = "EUR"
case USD = "USD"
static let allValues = [EUR, USD]
}
// MARK: - Confirm
func confirm(sendPayment intent: INSendPaymentIntent, completion: @escaping (INSendPaymentIntentResponse) -> Void) {
// success
completion(INSendPaymentIntentResponse(code: INSendPaymentIntentResponseCode.success, userActivity: nil))
}
// MARK: - Handle
func handle(sendPayment intent: INSendPaymentIntent, completion: @escaping (INSendPaymentIntentResponse) -> Void) {
// just for test
completion(INSendPaymentIntentResponse(code: .failureRequiringAppLaunch, userActivity: userActivity))
}
5) 你可以通过Siri启动:如果你选择了人民币或其他非常规货币,然后在代码中让你选择欧元或美元,但在RESOLVE函数中(当Siri想要解决更多货币时调用),你将得到人民币(因此你不需要添加任何像David所要求的按钮代码,因为所有按钮界面都将由Siri提供)。
Button
或其他接收用户交互的元素的代码。 - David Seek