3

I am trying to get the country code from iPhone user. I am not sure is there any information about the country and how some applications (like Viber) suggest dial code.

I got this far:

    let currentLocale = NSLocale.currentLocale()
    let a = currentLocale.localeIdentifier

I tested this on simulator and it returns "en_US". Can someone advice how should I get dial code for a country based on this (or at least country code, which I will send to server to get dial code).

Thanks!

Bob
  • 8,392
  • 12
  • 55
  • 96
  • It seems you have to get the country's ISO code, and look up the dial code from that. http://stackoverflow.com/questions/25095778/ios-how-to-find-country-code-of-the-users-phone-number – Pekka Nov 03 '15 at 20:32
  • Thanks! I will add answer based on that question. Since they answered for objective C – Bob Nov 03 '15 at 20:39

1 Answers1

18

Based on suggestion from Pekka, this is how country identifier can be loaded:

   // Setup the Network Info and create a CTCarrier object
   let networkInfo = CTTelephonyNetworkInfo()
   let carrier = networkInfo.subscriberCellularProvider

   // Get carrier name
   let countryCode = carrier?.isoCountryCode
   return countryCode

Also, it is important to import CoreTelephony class

import CoreTelephony

After that dial code for a country can be get from http://country.io/data/

Also be aware that this will not work on iOS simulator, since it doesn't provide carrier.

Bob
  • 8,392
  • 12
  • 55
  • 96