iOS
device location
country detection
mobile development
location services

Get device location only country in iOS

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you only need the country in iOS, the first question is whether you need the user's actual physical country or just the device's configured region. Those are different problems, and only one of them needs Core Location permission.

If Region Setting Is Enough

Sometimes "country" really means the device's current regional setting, not GPS-based location. In that case, do not ask for location at all. Use Locale:

swift
1import Foundation
2
3if let regionCode = Locale.current.region?.identifier {
4    print(regionCode) // for example, "CA" or "US"
5}

This is the privacy-friendly answer when the app only needs region-based formatting, content selection, or a default country picker value.

If You Need the Physical Country

If you truly need the user's current country from their physical location, there is no special "country-only" location API. You still request location permission, obtain coordinates, and reverse geocode them into a placemark.

swift
1import CoreLocation
2
3final class CountryResolver: NSObject, CLLocationManagerDelegate {
4    private let manager = CLLocationManager()
5    private let geocoder = CLGeocoder()
6
7    override init() {
8        super.init()
9        manager.delegate = self
10    }
11
12    func requestCountry() {
13        manager.requestWhenInUseAuthorization()
14        manager.requestLocation()
15    }
16
17    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
18        guard let location = locations.first else { return }
19
20        geocoder.reverseGeocodeLocation(location) { placemarks, error in
21            guard error == nil, let placemark = placemarks?.first else { return }
22            print(placemark.country ?? "Unknown")
23            print(placemark.isoCountryCode ?? "Unknown")
24        }
25    }
26
27    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
28        print(error.localizedDescription)
29    }
30}

This gives you the country after a one-shot location request rather than continuous tracking.

Required Privacy Setup

If you use Core Location, add the usage description key to Info.plist:

xml
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to detect your current country.</string>

Without that key, the app cannot request location authorization correctly.

Stop at the Minimum Necessary

If country is all you need, do not keep the location manager running. Request one location, reverse geocode it, and stop. That reduces battery use and keeps the privacy footprint smaller.

This is an important design point: iOS permissions are coarse compared with your business need, so it is your app's job to minimize what it does after permission is granted.

Reverse Geocoding Is the Real Country Step

Coordinates alone do not give you a country label. The country comes from reverse geocoding through CLGeocoder, which returns a CLPlacemark. Useful fields include:

  • 'country for the human-readable country name'
  • 'isoCountryCode for the stable two-letter country code'

If the app uses country for backend logic, the ISO code is usually safer than the localized display name.

Handle Failure Paths Gracefully

Location permission can be denied, geocoding can fail, and a one-shot location request can time out. The app should be ready to fall back to a manual country picker or a locale-based default instead of treating country detection as guaranteed.

Common Pitfalls

  • Requesting location permission when Locale.current.region would have been enough.
  • Assuming iOS has a country-only permission tier for physical location.
  • Forgetting the Info.plist usage description key.
  • Starting continuous location updates when a one-shot request is enough.
  • Using the display name of the country when the backend really needs an ISO country code.

Summary

  • First decide whether you need region settings or actual physical country.
  • Use Locale if device-region information is enough and avoid location permission entirely.
  • Use Core Location plus reverse geocoding if you need the user's current physical country.
  • Request one location and stop instead of tracking continuously.
  • Prefer isoCountryCode for stable application logic.

Course illustration
Course illustration

All Rights Reserved.