1

i'm using the library "PhoneNumberKit" to format a UITextfield i'm using on my app that i use it to input phone number.

i am using PartialFormatter to format the input into a phone number mask style.

@IBAction func onPhoneNumberChanged(_ sender: Any) {

        if (phoneNumberTextField.text?.count ?? 0 < 15) {
            phoneNumberTextField.text = PartialFormatter().formatPartial(phoneNumberTextField.text ?? "")
        } else {
            phoneNumberTextField.deleteBackward()
        }


    }

my problem is that it always format the string to a LOCAL phone number, i want to force it to format it into a US phone number mask.

the library say:

The default region code is automatically computed but can be overridden if needed.

but doesn't give any examples on force region code to be to a specific country

here is the library i'm using: https://github.com/marmelroy/PhoneNumberKit

does someone here has any exmaples on how to do it ?

thanks

Chief Madog
  • 1,738
  • 4
  • 28
  • 55

2 Answers2

2

fixed it by doing:

    phoneNumberTextField.text = PartialFormatter(phoneNumberKit: PhoneNumberKit(), defaultRegion: "US", withPrefix: true).formatPartial(phoneNumberTextField.text ?? "")
Chief Madog
  • 1,738
  • 4
  • 28
  • 55
-1

The default region code is automatically computed but can be overridden if needed like given below

class MyTextField: PhoneNumberTextField {
    override var defaultRegion: String {
        get {
            return "US"
        }
        set {} // exists for backward compatibility
    }
}

Reference

ajay_nasa
  • 2,278
  • 2
  • 28
  • 45