Pau*_* T. 12 ios siri swift ios10 sirikit
在我的申请中,我只支持欧元和美元货币.因此,当用户尝试使用Siri向GBP发送付款时,我要求他在EUR和USD之间进行选择.
之后在屏幕上我看到:
如果我选择100美元intent.currencyAmount!.currencyCode
我总是有英镑(但用户选择美元).这很奇怪.
这是我的代码:
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]
}
Run Code Online (Sandbox Code Playgroud)
更新:如何重现(根据大卫的问题):
1)创建一个新的意图extantion
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))
}
Run Code Online (Sandbox Code Playgroud)
5)你可以用Siri推出:你会看到如果你选择中国货币或任何其他非常规货币然后我在代码中让你在EUR和USD之间做出选择,但之后在RESOLVE功能中(当siri想要时调用)在更多时间解决货币)您将获得中国货币(因此您不需要为David问的按钮添加任何代码,因为所有按钮界面将由Siri提供)
我创建了这个问题: Siri 和错误的货币
您所需要做的就是确认用户选择的货币。您的确认方法实施错误,您应该使用如下方式确认货币:
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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
982 次 |
最近记录: |