MojtabaHs / iPhoneNumberField

Elegant SwiftUI phone number textField.
MIT License
528 stars 87 forks source link

How to limit the length depending on the currently selected country? #112

Closed davidkessler-ch closed 1 week ago

davidkessler-ch commented 1 week ago

How can I set a maxDigits value that is given by the longest possible valid phone number of the selected country?

I have tried:

iPhoneNumberField(nil, text: $number, isEditing: $editing) {
    $0.maxDigits = PhoneNumberKit()
                        .possiblePhoneNumberLengths(forCountry: $0.currentRegion, phoneNumberType: .fixedOrMobile, lengthType: .national)
                        .sorted(by: { $0 > $1 })
                        .first
}
davidkessler-ch commented 1 week ago

Turns out I just chose the wrong phoneNumberType and lengthType. It would still be nice to have a way of getting the longest out of all options with a single call.