尝试编写INSendPaymentIntent代码,但无法区分共享相似名字的联系人. Siri似乎在INPersonResolutionResult.disambiguation(with:matchedContacts)后立即进入循环
守则背后的思考
我选择使用联系人的姓名来初始搜索联系人,因为如果用户仅指定名字,则使用INPerson显示名称将返回与查询匹配的第一个联系人. (即’支付凯文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会要求确认这个人的姓名:
completion(INPersonResolutionResult.needsValue())
或这个:
completion(INPersonResolutionResult.disambiguation(with: matchedContacts))
在这种情况下,我认为它更有可能进入循环,因为你一直返回第二个结果(INPersonResolutionResult.disambiguation).这意味着您在此行中的查询将返回2人或更多人:
let searchContactsResult = try CNContactStore().unifIEdContacts(matching: predicate,CNContactIDentifIErKey])
我建议你调试那行,看看你是否给过Siri这个值:
INPersonResolutionResult.success总结
以上是内存溢出为你收集整理的swift – 如何在INSendPaymentIntent(SiriKit)中搜索特定联系人?全部内容,希望文章能够帮你解决swift – 如何在INSendPaymentIntent(SiriKit)中搜索特定联系人?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)