3

I want to detect region and if it is not Spain I need to create check like this:

if myRegion == Spain {

    //some code

} else {

    //some code

}

I trying to do this like this but it is doesn't work:

let myRegion = Locale.current

if myRegion == Locale(identifier: "sp") {
    //some code
} else {
    //some code
}

Update

if myRegion == Locale(identifier: "es") {
    print("YES")
} else {
    print("NO")
} 

let locale = Locale.current
print(locale.regionCode)

It is doesn't work. In console I see:

NO
Optional("ES")

If I change es to ES it is is doesn't work.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
user
  • 41
  • 5

2 Answers2

6
 let myRegion = Locale.current.regionCode
 if myRegion == "ES" {
        //some code 
    } else {
        //s
    } 

Spain region code is "es".You can find the list of codes here : LINK

If you are in same country, the easiest way to find out the region code is:

let locale = Locale.current
print(locale.regionCode)
Keshu R.
  • 5,045
  • 1
  • 18
  • 38
  • @user and also you need to declare `let myRegion = Locale.current.regionCode` not `let myRegion = Locale.current` – Keshu R. Jan 23 '20 at 09:34
  • Done. But I have error `Cannot assign value of type 'Locale' to type 'String?` – user Jan 23 '20 at 09:36
0

You can use Locale.current.regionCode.

n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35
Andrew Kochulab
  • 325
  • 2
  • 9