4

概要
a をコーディングしようとしていますINSendPaymentIntentが、名前が似ている連絡先を区別するのに苦労しています。Siriは直後にループするようですINPersonResolutionResult.disambiguation(with: matchedContacts)

コードの背後 にある考えユーザーが名前のみを指定した場合、表示名を使用するとクエリに一致する最初の連絡先が返されるため、最初に連絡先
の名を使用して連絡先を検索することにしました。(つまり、'Pay Kevin $50' は、Kevin Spacey ではなく Kevin Bacon を自動的に選択します)INPerson

残念ながら、与えられた名前を使用すると、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())
    }
}
4

1 に答える 1