在我的申请中,我只支持欧元和美元的货币.所以当用户尝试用Siri向GBP发送付款,例如,我请他选择欧元和美元.
之后在屏幕上我看到:
> 100 $
> 100欧元
如果我在intent.currencyAmount!.currencyCode中选择100 $,我总是有GBP(但是用户选择了美元).这很奇怪
这是我的代码:
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] }
更新:如何复制(根据大卫问题):
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)),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想要解决货币更多的时间)你会得到中国货币(所以你不需要添加任何代码的按钮像大卫问,因为所有的按钮接口将由Siri提供)
解决方法
我创建了这个问题:
Siri and wrong currency
Siri and wrong currency
所有您需要做的是确认用户选择的货币.
您的确认方法错误地执行,您应该使用以下方式确认货币:
let response = INSendPaymentIntentResponse(code: .ready,userActivity: nil) response.paymentRecord = INPaymentRecord( payee: intent.payee,payer: nil,currencyAmount: intent.currencyAmount,paymentMethod: nil,note: nil,status: .pending,feeAmount: nil) completion(response)