【发布时间】:2016-10-24 09:34:51
【问题描述】:
在我的应用程序中,我只支持欧元和美元货币。因此,例如,当用户尝试使用 Siri 向英镑付款时,我要求他在欧元和美元之间进行选择。
之后我在屏幕上看到:
- 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)), 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 启动:你会看到,如果你选择中国货币或任何其他非常规货币,然后我在代码中让你在 EUR 和 USD 之间进行选择,但之后在 RESOLVE 函数中(调用时调用siri 想在更多时间解析货币)你会得到中国货币(所以你不需要像大卫问的那样为按钮添加任何代码,因为所有按钮界面都将由 Siri 提供)
【问题讨论】:
-
我没有看到剪断的短代码有错误。也许您想发布
Button的代码或任何需要用户交互的元素。 -
没有错。我认为这可能是苹果的错误,因为很奇怪,如果我选择 USD ,我会立即从 Siri 获得 GBP 货币
-
好吧,但是如果你不想发布代码,假设你是对的,并且进一步假设苹果框架中存在一个错误,你对这个线程有什么期望?跨度>
-
我为您更新了说明。如您所见,您不需要任何按钮代码,因为所有按钮界面都将由 Siri 提供。因此,您可以尝试重现相同的问题,但在您需要创建 Intents 扩展之前。如果 Apple 框架中存在错误,我认为可能有任何解决方法,可以让代码正常工作
标签: ios swift ios10 siri sirikit