摘要
尝试编写 a 代码,INSendPaymentIntent
但在区分具有相似名字的联系人时遇到了问题。Siri似乎在之后就进入了循环INPersonResolutionResult.disambiguation(with: matchedContacts)
代码背后的想法
我最初选择使用联系人的名字来搜索联系人,因为INPerson
如果用户只指定名字,则使用显示名称会返回与查询匹配的第一个联系人。(即“支付凯文 50 美元”将自动选择凯文培根而不是凯文史派西)。
不幸的是,使用给定的名字会使 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())
}
}