【发布时间】:2016-12-10 14:44:05
【问题描述】:
总结
尝试编写 INSendPaymentIntent 代码,但在区分具有相似名字的联系人时遇到问题。 Siri 似乎在 INPersonResolutionResult.disambiguation(with: matchedContacts) 之后进入了一个循环
代码背后的想法
我最初选择使用联系人的名字来搜索联系人,因为如果用户只指定名字,则使用INPerson 显示名称会返回与查询匹配的第一个联系人。 (即“支付 Kevin 50 美元”将自动选择 Kevin Bacon 而不是 Kevin Spacey)。
不幸的是,使用给定名称会使 Siri 进入一个循环,要求用户一遍又一遍地指定联系人...
问题
有没有什么方法可以使用联系人的名字搜索联系人而不会让 Siri 进入循环?
代码
func resolvePayee(forSendPayment intent: INSendPaymentIntent, with completion: (INPersonResolutionResult) -> Void) {
if let payee = intent.payee {
var resolutionResult: INPersonResolutionResult?
var matchedContacts: [INPerson] = []
let predicate = CNContact.predicateForContacts(matchingName: (payee.nameComponents?.givenName)!)
do {
let searchContactsResult = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactIdentifierKey])
for contact in searchContactsResult {
matchedContacts.append(createContact((contact.phoneNumbers.first?.value.stringValue)!, contact: contact))
}
} catch {
completion(INPersonResolutionResult.unsupported())
}
switch matchedContacts.count {
case 2 ... Int.max:
resolutionResult = INPersonResolutionResult.disambiguation(with: matchedContacts)
case 1:
let recipientMatched = matchedContacts[0]
print("Matched a recipient: \(recipientMatched.displayName)")
resolutionResult = INPersonResolutionResult.success(with: recipientMatched)
case 0:
print("This is unsupported")
resolutionResult = INPersonResolutionResult.unsupported()
default:
break
}
completion(resolutionResult!)
} else {
completion(INPersonResolutionResult.needsValue())
}
}
【问题讨论】:
-
当你说“使用给定的名字让 Siri 进入一个循环,要求用户一遍又一遍地指定联系人”时,这是否意味着你正在点击 switch 语句的 case 0?
-
@rocky 不,是情况 1。日志打印显示收件人匹配,但 Siri 再次提出问题。
-
您确定 recipientMatched 是 INPerson 类型吗?
-
@rocky 是的,这可能是 Siri 的一个错误
-
@cyril 你找到解决方案了吗?