Phone number format from country code

I need to display the phone number format as a placeholder in a UITextField . Please let me know how I can do this.

To select a country, I use the library below, and it provides me with the country flag and country code for the selected country.

https://github.com/NikKovIos/NKVPhonePicker

After choosing a country, I need to display the phone number format for the selected country, and when sending this phone number I must confirm the phone number.

I also found that a third-party ( PhoneNumberKit ), which is inspired by google libphonenumber, but it is designed to be validated, does not provide the expected phone number format by country code. Below is the link.

https://github.com/marmelroy/PhoneNumberKit

thank

Update 1: Tried this and got a Common Parser Error

let phoneNumberKit = PhoneNumberKit()

        do {
            let phoneNumber = try phoneNumberKit.parse("+921230123456")

        }
        catch {
            print("Generic parser error")
        }

Update 2: Updated code still getting exception

let phoneNumberKit = PhoneNumberKit()

        do {
            let phoneNumber = try phoneNumberKit.parse("1230123456", withRegion: "FR", ignoreType: false)
            let formatedNumber = phoneNumberKit.format(phoneNumber, toType: .international)
            print(formatedNumber)
        }
        catch {
            print("Generic parser error")
        }
+1
source share
2 answers

For those who want to do the same, I used two different sides to achieve this functionality.

NKVPhoneNumber is used to select the country code, I changed it a bit phone_formatin the metadata. After selecting a country from the list, it returns an object Countrythat includesCode, Extension, Flag and format_placeholder

SHSPhoneComponent , then use format_placeholderto verify the format.

import SHSPhoneComponent
import NKVPhonePicker

@IBOutlet weak var phoneTF: SHSPhoneTextField!
@IBOutlet weak var phoneFlag: NKVPhonePickerTextField!
@IBOutlet weak var lblCountryCode: UILabel!

//MARK: - NKV callback delegates

func countriesViewController(_ sender: CountriesViewController, didSelectCountry country: Country) {
    //

    phoneTF.formatter.setDefaultOutputPattern(country.formatPattern)
    phoneTF.text = ""
    phoneTF.placeholder = country.formatPatternPlaceHolder
    countryCode = "+\(country.phoneExtension)"

    lblCountryCode.text = countryCode
}

. NVKPhoneNumber Swift 4.0,

0

, ,

, 012345679, ,

  • , .
  • .
+2

Source: https://habr.com/ru/post/1529390/


All Articles