I have found the answer for this for objective-c but Im having a hard time doing this in swift.
I have used this to get the country code for the current location:
let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
print(countryCode)
// printing for example US
But how do I convert this country code to a country name, like in this example converting "US" to "United States"?
ISO 3166-1 alpha-3 codes are three-letter country codes defined in ISO 3166-1, part of the ISO 3166 standard published by the International Organization for Standardization (ISO), to represent countries, dependent territories, and special areas of geographical interest.
Member countries of the North American Numbering Plan (NANP) are assigned three-digit area codes under the common country prefix 1, shown in the format +1 XXX. +1 – United States, including United States territories: +1 340 – United States Virgin Islands.
A super clean Swift 3 version would be:
func countryName(countryCode: String) -> String? {
let current = Locale(identifier: "en_US")
return current.localizedString(forRegionCode: countryCode)
}
You can change the locale identifier to eg. Locale.current.identifier if you want localized names. The example above is for English only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With