1

I tried using PhoneNumberKit and couldn't find any proper api which would give me the region name. I need the region from the phone number so that I can display the appropriate flag. For example using this:

  let phoneNumber = try phoneNumberKit.parse("+12563335956")
  let regionCode = phoneNumberKit.countries(withCode: phoneNumber.countryCode)?.first
  print("region code is: " , regionCode) 

// US phone number with +1 prefix, but it prints "AG" which is wrong.

Enrik Qaz
  • 243
  • 4
  • 14

1 Answers1

3

As you said, the country code can tell you which flag to use.

let phoneNumber = try phoneNumberKit.parse("+1 970162651778")
let regionCode = phoneNumberKit.getRegionCode(of: phoneNumber)

print(regionCode) // Optional("US")

You can get the country region code using the country code of the phone number.

Callam
  • 11,409
  • 2
  • 34
  • 32
  • Check edited question. This doesn't work. It would print "AG" – Enrik Qaz Oct 05 '18 at 10:51
  • @EnrikQaz it wasn't working because there are multiple countries that use +1, however, country codes that are shared by multiple countries do have a main country, which you can get using `getRegionCode`. – Callam Oct 05 '18 at 11:20