Get device location (only country) in iOS

2019-01-08 06:10发布

I need to get the country location of a iOS device.

I've been trying to use CoreLocation with MKReverseGeocoder. However this seems to return erraneous quite frequently. And I only need the country, no need for streets and such.

How can this be done in a more stable way?

12条回答
叼着烟拽天下
2楼-- · 2019-01-08 06:34
NSLocale *countryLocale = [NSLocale currentLocale];  
NSString *countryCode = [countryLocale objectForKey:NSLocaleCountryCode];
NSString *country = [countryLocale displayNameForKey:NSLocaleCountryCode value:countryCode];
NSLog(@"Country Locale:%@  Code:%@ Name:%@", countryLocale, countryCode, country);
//Country Locale:<__NSCFLocale: 0x7fd4b343ed40>  Code:US   Name:United States
查看更多
对你真心纯属浪费
3楼-- · 2019-01-08 06:35

Here is @Denis's and @Matt's answers put together for a Swift 3 solution:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()
    let geoCoder = CLGeocoder()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.requestAlwaysAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.startMonitoringSignificantLocationChanges()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let currentLocation = locations.first else { return }

        geoCoder.reverseGeocodeLocation(currentLocation) { (placemarks, error) in
            guard let currentLocPlacemark = placemarks?.first else { return }
            print(currentLocPlacemark.country ?? "No country found")
            print(currentLocPlacemark.isoCountryCode ?? "No country code found")
        }
    }
}

Don't forget to set the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in Info.plist as well!

查看更多
Melony?
4楼-- · 2019-01-08 06:39
NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];

will get you an identifier like e.g. "US" (United States), "ES" (Spain), etc.


In Swift 3:

let countryCode = NSLocale.current.regionCode

In Swift 2.2:

let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as String

Compared to a solution based on CLLocationManager this approach has pros and cons. The primary con is that it doesn't guarantee that this is where the device is physically if the user configures it differently. This can however also be seen as a pro since it instead shows which country a user is mentally/culturally aligned with - so if e.g. I go abroad on vacation then the locale is still set to my home country. However a pretty big pro is that this API doesn't require user permission like CLLocationManager does. So if you haven't already gotten permission to use the user's location, and you can't really justify throwing a popup dialog in the user's face (or they already rejected that popup and you need a fallback) then this is probably the API you want to use. Some typical use cases for this could be personalization (e.g. culturally relevant content, default formats, etc.) and analytics.

查看更多
放我归山
5楼-- · 2019-01-08 06:42

Swift 4.0 code for getting the Country name as per region set:

    let countryLocale = NSLocale.current
    let countryCode = countryLocale.regionCode
    let country = (countryLocale as NSLocale).displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
    print(countryCode, country)

prints: Optional("NG") Optional("Nigeria"). //for nigeria region set

查看更多
甜甜的少女心
6楼-- · 2019-01-08 06:49

You can get NSTimeZone from CLLocation: https://github.com/Alterplay/APTimeZones and works locally.

查看更多
戒情不戒烟
7楼-- · 2019-01-08 06:49

@Rob

let locale = Locale.current
let code = (locale as NSLocale).object(forKey: NSLocale.Key.countryCode) as! String?

using these code you will get your region country code and if you didn't get still then change it just go in Phone setting->general->language & region and set your region you want

查看更多
登录 后发表回答