iosobjective-cnslocale

Easy way to check for continents using NSLocale, NSLocaleCountryCode?


Is there a way to check if the user's current country is in Europe, Asia or Africa?

I working on an app that does something different for the UK and the US. I want to extend that logic so if the user's locale is set to a country in Europe, then I default to what I do for the UK. If it's outside Europe, UK, then I default to what I do for the US.

This is what I do at the moment.

NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
if ([countryCode isEqualToString:@"UK"]) {
    NSString *startString = @"£ ";
}
if ([countryCode isEqualToString:@"US"]) {
    NSString *startString = @"$ ";
}

Is there a simple way to check for continents?


Solution

  • You can get the name of the continent this way...

     NSTimeZone *timeZone = [NSTimeZone localTimeZone];
     NSString *tzName = [timeZone name];
    

    Now you can handle what you need with the name (switch statements). For example in Chicago it shows: America/Chicago

    Another Option

    Use iOSCowboy where you can use the country code (ISO 3166 Format) to return the continent.

    One more option

    You can create an array that has the countries and their continent...