Bob
Bob

Reputation: 8704

Swift 2 country code for a phone number

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!

Upvotes: 3

Views: 6280

Answers (1)

Bob
Bob

Reputation: 8704

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.

Upvotes: 18

Related Questions