【问题标题】:How can I choose phone number in INSendPaymentIntent如何在 INSendPaymentIntent 中选择电话号码
【发布时间】:2017-03-05 11:48:08
【问题描述】:

在我的应用程序中,我需要使用用户的电话号码(而不是用户的联系人姓名)发送付款。因此,如果联系人中的用户有 2 个电话号码,则用户必须选择其中一个号码(例如在电话呼叫意图中)。

func resolvePayee(forSendPayment intent: INSendPaymentIntent, with completion: @escaping (INPersonResolutionResult) -> Void) 函数中,我只看到用户名、姓氏,但没有电话号码。甚至intent.payee.personHandle always == nil

【问题讨论】:

  • 嗨!如果收件人有两个号码,您是否找到解决方案如何选择号码?我也有同样的问题。如果你解决了它,你可以发布你的问题的答案吗?
  • @dasha ,我添加了答案,我认为这不是最好的方法,但它有效

标签: ios iphone swift ios10 sirikit


【解决方案1】:

我使用了这项工作(只需在显示联系人姓名时添加号码)。 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())
        }
    }

【讨论】:

  • 感谢您的回复!当你有几个号码的人时,Siri 到底会问什么? “你要选哪个号码?”或“哪个名字?”
  • 我不记得了,但不记得“哪个名字”。诸如“选择之间”之类的东西(现在不能尝试,因为我暂时删除了 siri 支持,因为我无法将存档上传到苹果,因为我的可可豆荚有问题)
  • 也许这不是最好的解决方案,但我也没有找到更好的解决方案:D 谢谢!
猜你喜欢
  • 2017-05-28
  • 1970-01-01
  • 2011-10-25
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 2014-08-24
相关资源
最近更新 更多