0

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?

gobob
  • 412
  • 1
  • 4
  • 17

2 Answers2

5

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...

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • 1
    This solution is not ideal in many cases. You can't differentiate between North and South america, for example. But it may be enough to check for European timezones. – rmaddy Jan 02 '16 at 02:17
  • 1
    @rmaddy Good point. For my purpose, timezones are good enough. For UK, Europe is +1 to +4 GMT. And for remainder of the world, I can the US. – gobob Jan 02 '16 at 02:59
2

You can parse the time zone to determine a rough approximation of the current continent.

For the complete list of time zones, you can run sudo systemsetup -listtimezones from macOS.

public extension TimeZone {
    var globeEmoji: String {
        let region = self.identifier.split(separator: "/").first ?? ""
        switch region {
        case "Atlantic",
             "Africa",
             "Europe",
             "GMT":
            return ""
        case "Arctic",
             "Asia",
             "Australia",
             "Indian":
            return ""
        case "America",
             "Antarctica",
             "Pacific":
            return ""
        case "Brazil",
             "CET",
             "CST6CDT",
             "Canada",
             "Chile",
             "Cuba",
             "EET",
             "EST",
             "EST5EDT",
             "Egypt",
             "Eire",
             "Etc",
             "Factory",
             "GB",
             "GB-Eire",
             "GMT+0",
             "GMT-0",
             "GMT0",
             "Greenwich",
             "HST",
             "Hongkong",
             "Iceland",
             "Iran",
             "Israel",
             "Jamaica",
             "Japan",
             "Kwajalein",
             "Libya",
             "MET",
             "MST",
             "MST7MDT",
             "Mexico",
             "Midseast",
             "NZ",
             "NZ-CHAT",
             "Navajo",
             "PRC",
             "PST8PDT",
             "Poland",
             "Portugal",
             "ROC",
             "ROK",
             "Singapore",
             "Turkey",
             "UCT",
             "US",
             "UTC",
             "Universal",
             "U-SU",
             "WET",
             "Zulu":
            print("An unusual time zone was detected. These are not usually used.")
            return ""
        default:
            print("Failed to handle time zone: \(self.identifier)")
            return ""
        }
    }
}
Isaiah Turner
  • 2,574
  • 1
  • 22
  • 35