如何在INSendPaymentIntent(SiriKit)中搜索特定联系人?

时间:2016-08-04 12:55:39

标签: swift ios10 sirikit

摘要
尝试对INSendPaymentIntent进行编码,但在区分具有相似名字的联系人时遇到问题。 Siri似乎在INPersonResolutionResult.disambiguation(with: matchedContacts)

后立即进入循环

守则背后的想法
我选择使用联系人的给定名称来搜索联系人,因为如果用户仅指定名字,则使用INPerson显示名称将返回与查询匹配的第一个联系人。 (即'Pay 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())
    }
}

1 个答案:

答案 0 :(得分:1)

Siri会在您退回时确认该人的姓名:

completion(INPersonResolutionResult.needsValue())

或者这个:

completion(INPersonResolutionResult.disambiguation(with: matchedContacts))

在这种情况下,我认为它更有可能进入循环,因为你不断返回第二个结果(INPersonResolutionResult.disambiguation)。这意味着您在此行中的查询将返回2人或更多人:

let searchContactsResult = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch:[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactIdentifierKey])

我建议你调试那条线,看看你是否给过Siri这个值:

INPersonResolutionResult.success