【问题标题】:How do I search for a specific contact in INSendPaymentIntent (SiriKit)?如何在 INSendPaymentIntent (SiriKit) 中搜索特定联系人?
【发布时间】: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 你找到解决方案了吗?

标签: swift ios10 sirikit


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 2013-10-10
    • 2011-08-31
    • 2012-02-21
    相关资源
    最近更新 更多