Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get country code from country name in IOS

Tags:

Im retrieving a country name from a server in english. For example, "Spain"

What I want to do is, assuming the country name is going to be written in english, get the country code.

What should I do? I´ve found getting the country name from the country code quite easy, but I´ve got no idea of how to do the opposite operation.

Thanks a lot.

like image 769
pdrcabrod Avatar asked Oct 01 '12 10:10

pdrcabrod


People also ask

How do I get an iOS country code?

Take a look at below code it will help you to do so. NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale. NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; // get country code, e.g. ES (Spain), FR (France), etc.

How do you find the country code of a country?

Locale loc = new Locale("","NL"); loc. getDisplayCountry();

What does country code mean on an application?

Country codes are short alphabetic or numeric geographical codes (geocodes) developed to represent countries and dependent areas, for use in data processing and communications. Several different systems have been developed to do this.


1 Answers

Jef's answer helped here, with slight additions.

NSArray *countryCodes = [NSLocale ISOCountryCodes]; NSMutableArray *countries = [NSMutableArray arrayWithCapacity:[countryCodes count]];  for (NSString *countryCode in countryCodes) {     NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];     NSString *country = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"] displayNameForKey: NSLocaleIdentifier value: identifier];     [countries addObject: country]; }  NSDictionary *codeForCountryDictionary = [[NSDictionary alloc] initWithObjects:countryCodes forKeys:countries]; 

Now go through the 'codeForCountryDictionary' with the name of the country for which you require the code, for example,

NSLog(@"%@",[codeForCountryDictionary objectForKey:@"Spain"]); 

would yield the result as 'ES', which is the 2 letter country code for spain.

like image 158
Breakpoint Avatar answered Oct 12 '22 05:10

Breakpoint