2016-08-04 54 views
4

摘要
试图编码INSendPaymentIntent,但在区分具有相似名字的联系人时遇到问题。 Siri的似乎走在一个循环之后INPersonResolutionResult.disambiguation(with: matchedContacts)如何搜索INSendPaymentIntent(SiriKit)中的特定联系人?

思考
我选择最初使用联系人的给定的名称来搜索联系人,因为使用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()) 
    } 
} 
+0

当你说“使用给定的名称将Siri发送到一个循环中,要求用户反复指定联系人“,这是否意味着您正在切换语句的情况0? – rocky

+0

@rocky nope它的情况1.日志打印,收件人匹配,但Siri再次提出问题。 – cyril

+0

您确定recipientMatched是INPerson类型吗? – rocky

回答

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