2016-10-23 9 views
2

私のアプリケーションでは、ユーザーの電話番号(ユーザーの連絡先名ではない)を使用して支払いを送信する必要があります。連絡先のユーザーが2つの電話番号を持っている場合、ユーザーは電話番号のように番号の1つを選択する必要があります。INSendPaymentIntentで電話番号を選択する方法

ファンクションfunc resolvePayee(forSendPayment intent: INSendPaymentIntent, with completion: @escaping (INPersonResolutionResult) -> Void)ユーザー名、姓のみが表示されますが、電話番号は表示されません。偶数intent.payee.personHandle always == nil

+0

こんにちは!受取人が2つの番号を持っている場合、どのように番号を選ぶか、解決法が見つかりましたか?私は同じ問題を抱えています。あなたが解決したら、あなたの質問に答えを投稿してください。 – birdy

+0

@dasha、私は答えを加えた、私はそれが最良の方法ではないと思うが、それは動作します –

答えて

2

私はこの回避策を使用しました(私はそれらを表示するときに連絡先の名前に番号を付けてください)。ここにresolvePayee関数の完全なコードがあります:

func resolvePayee(forSendPayment intent: INSendPaymentIntent, with completion: @escaping (INPersonResolutionResult) -> Void) { 

     print("start resolving payee") 

     if let payee = intent.payee, payee.displayName.length > 0 { 

      // search proper phone number if we chose between these numbers before 
      var foundPhoneNumberInBuferNumbers: String? = nil 
      if let arrayOfSavedPhones = PaymentIntentHandler.resolvePayeeHelp_offeredPhoneNumbersFor1Contact { 
       for phone in arrayOfSavedPhones { 
        if payee.displayName.hasSuffix(phone) { 
         foundPhoneNumberInBuferNumbers = phone 
         break 
        } 
       } 
       if foundPhoneNumberInBuferNumbers == nil { // if we didn't manage to find this number that means that we have a new request for a new contact, so we don't need to remember number in buffer anymore 
        print("can't find phone number for \(payee.displayName), so we remove all buffer numbers = \(PaymentIntentHandler.resolvePayeeHelp_offeredPhoneNumbersFor1Contact)") 
        PaymentIntentHandler.resolvePayeeHelp_offeredPhoneNumbersFor1Contact = nil 
       } 
      } 

      let searchName = foundPhoneNumberInBuferNumbers != nil ? payee.displayName.replacingOccurrences(of: " " + foundPhoneNumberInBuferNumbers!, with: "") : payee.displayName 

      Singleton.sharedInstance.contactsService_getPhoneContacts(contactIdentifiresToFetch: nil, matchingName: searchName, showAlert: false, completionBlock: { (havePermission, customContactsMatchingName) in 
       if !havePermission { // user didn't grant permissions 
        print("unsupported because don't have permissions") 
        completion(INPersonResolutionResult.unsupported()) 
        return 
       } 

       switch customContactsMatchingName.count { 
       case 2 ... Int.max: // user has to choose 
        let arrayOfPersons = customContactsMatchingName.map({ (phoneBookContact) -> INPerson in 
         return phoneBookContact.inSiriPerson() 
        }) 
        print("disambiguation between several contacts") 
        completion(INPersonResolutionResult.disambiguation(with: arrayOfPersons)) 
       case 1: 
        let contactCustom = customContactsMatchingName[0] 
        switch contactCustom.phoneNumbers.count { 
        case 0: 
         print("unsupported because don't have phone numbers") 
         completion(INPersonResolutionResult.unsupported()) 
        case 1: 
         print("success") 
         completion(INPersonResolutionResult.success(with: contactCustom.inSiriPerson())) 
        case 2 ... Int.max: 

         print("now we have a uniq contact, but several phone numbers") 

         if foundPhoneNumberInBuferNumbers == nil { // we need user to choose between several phone numbers 

          var arrayOfStringNumbers = [String]() 
          var arrayOfPersonsToChoose = [INPerson]() 
          for phoneNumber in contactCustom.phoneNumbers { 
           let numberOnlyDigits = Singleton.sharedInstance.phone_getPhoneOnlyDigits(phoneNumber.number) 
           let formattedNumber = Singleton.sharedInstance.phone_getFormattedPhone("+" + numberOnlyDigits, allowLetters: false) 
           arrayOfStringNumbers.append(formattedNumber) 
           let personHandler = INPersonHandle(value: formattedNumber, type: .phoneNumber) 
           let person = INPerson(personHandle: personHandler, nameComponents: nil, displayName: payee.displayName + " " + formattedNumber, image: nil, contactIdentifier: contactCustom.id, customIdentifier: formattedNumber) 
           arrayOfPersonsToChoose.append(person) 
          } 

          PaymentIntentHandler.resolvePayeeHelp_offeredPhoneNumbersFor1Contact = arrayOfStringNumbers 

          print("disambiguation in one contact between several phones") 
          completion(INPersonResolutionResult.disambiguation(with: arrayOfPersonsToChoose)) 
         } 
         else { 

          // found proper phone number 
          print("success after choosing bufer number") 
          let personHandle = INPersonHandle(value: foundPhoneNumberInBuferNumbers!, type: .phoneNumber) 
          let displayName = payee.displayName.replacingOccurrences(of: " " + foundPhoneNumberInBuferNumbers!, with: "") 
          completion(INPersonResolutionResult.success(with: INPerson(personHandle: personHandle, nameComponents: nil, displayName: displayName, image: nil, contactIdentifier: nil, customIdentifier: personHandle.value))) 
         } 


        default: 
         print("unsupported: can't happen") 
         completion(INPersonResolutionResult.unsupported()) 
        } 
       case 0: 
        print("unsupported because can't find this contact in phonebook = \(searchName), contacts in buffer = \(PaymentIntentHandler.resolvePayeeHelp_offeredPhoneNumbersFor1Contact)") 
        completion(INPersonResolutionResult.unsupported()) 
       default: 
        print("unsupported: can't happen") 
        completion(INPersonResolutionResult.unsupported()) 
       } 
      }) 
     } 
     else { 
      print("needsValue") 
      PaymentIntentHandler.resolvePayeeHelp_offeredPhoneNumbersFor1Contact = nil 
      completion(INPersonResolutionResult.needsValue()) 
     } 
    } 
+0

返信いただきありがとうございます!シリアがいくつかの数字を持つ人物を尋ねると正確に何を求めていますか? "どちらの番号を選びたいですか?"または "どの名前?" – birdy

+0

私は覚えていませんが、 "どの名前"を覚えていません。私がココアポッドに問題があったので、私はアップリケにアーカイブをアップロードすることに問題があったので、一時的にシリサポートを削除したので、今すぐ試すことはできません) –

+0

多分それは最善の解決策ではありませんが、私はよく分からなかった:Dありがとう! – birdy

関連する問題