3

I need to display phone number format as placeholder in UITextField. How can I do that?

For country selection I'm using below mentioned library and it provides me country flag and country code against user selected country.

https://github.com/NikKovIos/NKVPhonePicker

After selecting a country I need to display phone number format for that selected country and on submission of that phone number I have to validate the phone number.

I also find that third party (PhoneNumberKit) which is inspired by google's libphonenumber but it is for validating, it do not provide expected phone number format against country code. Below is the link.

https://github.com/marmelroy/PhoneNumberKit

Update 1: Tried this and getting Generic 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")
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Shahbaz Saleem
  • 317
  • 2
  • 14
  • Please take a look at the section [how to ask a question](https://stackoverflow.com/help/how-to-ask). And this can also be relevant. [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Rick Bronger Aug 10 '17 at 06:37
  • 5
    @RickBronger I already read this and my question is also valid, please don't discourage without trying to understand the question. This question was not related to your skill set, maybe that's why you couldn't understand. Otherwise i asked a very straight forward question and asked for suggestion to my problem and shared my findings with community. Also before posting i double checked that if someone have any solution or way to this problem. – Shahbaz Saleem Aug 10 '17 at 06:46
  • I didnt see any effort in your question to solve your problem. Neither a code snippet that you are stuck at a point where we can help. Your asking for someone to write code for you that is not how stackoverflow works in my opinion. – Rick Bronger Aug 10 '17 at 07:42
  • @RickBronger We always need a direction to put effort to do something, i couldn't find that way till now. It is not necessary post every try that is not leading in the right direction. See below answer and comments, you may find some effort on my problem. – Shahbaz Saleem Aug 10 '17 at 07:46

2 Answers2

2

I don't know whether this is a valid solution or not, you could try this

Say let your placeholder be 012345679 what I believe you could do is

  • Create a variable to store this placeholder
  • Parse this placeholder against the country that the user selects.
  • Set the parsed one as the placeholder in the textfield.
Aravind A R
  • 2,674
  • 1
  • 15
  • 25
1

For those who wanna do the same thing, I used two different 3rd parties to achieve the functionality.

  1. NKVPhoneNumber
  2. SHSPhoneComponent

NKVPhoneNumber is used to select country code, i've modified it a bit a introduced phone_format in the Meta Data. Once selected a country from the list it return a Country object which includes Code, Extension, Flag and format_placeholder

SHSPhoneComponent then use that format_placeholder for validation of 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
}

Note: I've converted NVKPhoneNumber to Swift 4.0,

Shahbaz Saleem
  • 317
  • 2
  • 14
  • the countriesViewController function is never called in my project. And I already put phoneFlag.phonePickerDelegate = self. Do I need to configure something else? – André Vicente Dec 07 '18 at 15:25
  • @AndréVicente I have the same problem and my app always crashes because the country flag is nil. Did you find a way around this issue? – Lance Samaria Feb 11 '19 at 15:25
  • if you can share your code somehow, i may help... On selected your flag is nil or somewhere else? – Shahbaz Saleem Feb 13 '19 at 07:08