Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中获取设备位置(仅国家/地区)_Ios_Core Location - Fatal编程技术网

在iOS中获取设备位置(仅国家/地区)

在iOS中获取设备位置(仅国家/地区),ios,core-location,Ios,Core Location,我需要获取iOS设备的国家/地区位置 我一直在尝试将CoreLocation与MKReverseGeocoder一起使用。然而,这种情况似乎经常出现。我只需要乡村,不需要街道之类的 如何以更稳定的方式实现这一点?NSLocale只是关于当前使用的区域设置的设置,并不意味着您所在的实际国家 使用CLLocationManager获取当前位置&CLGeocoder执行反向地理编码。你可以从那里得到国家名称 NSString *countryCode = [[NSLocale currentLocal

我需要获取iOS设备的国家/地区位置

我一直在尝试将CoreLocation与MKReverseGeocoder一起使用。然而,这种情况似乎经常出现。我只需要乡村,不需要街道之类的


如何以更稳定的方式实现这一点?

NSLocale
只是关于当前使用的区域设置的设置,并不意味着您所在的实际国家

使用
CLLocationManager
获取当前位置&
CLGeocoder
执行反向地理编码。你可以从那里得到国家名称

NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
将为您提供一个标识符,例如“US”(美国)、“ES”(西班牙)等


在Swift 3中:

let countryCode = NSLocale.current.regionCode
let countryCode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as String
在Swift 2.2中:

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

与基于CLLocationManager的解决方案相比,这种方法有优缺点。主要的缺点是,如果用户对设备进行不同的配置,它不能保证设备的物理位置。但是,这也可以被视为专业,因为它显示了用户在心理/文化上与哪个国家保持一致-因此,如果我出国度假,则区域设置仍然设置为我的祖国。然而,一个相当大的优点是,这个API不像CLLocationManager那样需要用户权限。因此,如果您尚未获得使用用户位置的权限,并且您无法真正证明在用户面前抛出弹出对话框是合理的(或者他们已经拒绝了该弹出对话框,您需要回退),那么这可能就是您想要使用的API。这方面的一些典型用例可能是个性化(例如文化相关内容、默认格式等)和分析。

如果您只对电话设备感兴趣,那么这里提到的技术可能对您有用:

这里有一种替代方法,可能过于迂回。其他解决方案基于手动设置(NSLocale)或请求使用可拒绝的位置服务的权限(CLLocationManager),因此它们有缺点

您可以根据当地时区获取当前国家/地区。我的应用程序正在与一台安装了pytz的运行Python的服务器连接,该模块提供了一个时区字符串的国家代码字典。我只需要让服务器知道国家,这样我就不必完全在iOS上进行设置。在Python方面:

>>> import pytz
>>> for country, timezones in pytz.country_timezones.items():
...     print country, timezones
... 
BD ['Asia/Dhaka']
BE ['Europe/Brussels']
BF ['Africa/Ouagadougou']
BG ['Europe/Sofia']
BA ['Europe/Sarajevo']
BB ['America/Barbados']
WF ['Pacific/Wallis']
...
在iOS方面:

NSTimeZone *tz = [NSTimeZone localTimeZone];
DLog(@"Local timezone: %@", tz.name); // prints "America/Los_Angeles"
我让我的服务器发送本地时区名称,并在pytz country_时区字典中查找它

如果您在pytz或其他来源中提供该词典的iOS版本,则可以使用它根据时区设置(通常是最新的)立即查找国家代码,而无需服务器的帮助


不过,我可能会误解你。它是否通过区域格式首选项或时区设置为您提供国家代码?如果是后者,那么这只是获得相同结果的一种更复杂的方法…

您可以从CLLocation:获得NtimeZone,并在本地工作。

@Denis的答案很好——下面是一些将他的答案付诸实践的代码。这适用于您已设置为符合
CLLocationManagerDelegate
协议的自定义类。它有点简化(例如,如果位置管理器返回多个位置,则只使用第一个位置),但应该给人们一个良好的开端

-(id)init//指定初始值设定项
{
如果(自我)
{
self.locationManager=[[CLLocationManager alloc]init];
self.geocoder=[[CLGeocoder alloc]init];
self.locationManager.delegate=self;
[self.locationManager启动监视重要位置更改];
}
回归自我;
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
如果(位置==零)
返回;
self.currentLocation=[locations objectAtIndex:0];
[self.geocoder reverseGeocodeLocation:self.currentLocation completionHandler:^(NSArray*放置标记,NSError*错误)
{
如果(placemarks==nil)
返回;
self.currentLocPlacemark=[placemarks objectAtIndex:0];
NSLog(@“当前国家:%@,[self.currentLocPlacemark country]);
NSLog(@“当前国家/地区代码:%@,[self.currentLocPlacemark ISOcountryCode]);
}];
}
NSLocale*countryLocale=[NSLocale currentLocale];
NSString*countryCode=[CountryLocaleObjectForkey:NSLocaleCountryCode];
NSString*country=[countryLocale displayNameForKey:NSLocaleCountryCode值:countryCode];
NSLog(@“国家/地区:%@代码:%@名称:%@”,国家/地区,国家/地区代码,国家/地区);
//国家/地区:代码:美国名称:美国

对于Swift 3,它更简单:

let countryCode = Locale.current.regionCode

以下是@Denis和@Matt的答案,它们共同构成了一个Swift 3解决方案:

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")
        }
    }
}

不要忘记在
Info.plist
中设置
NSLocationAlwaysUsageDescription
nsLocationWhenUsageDescription

在Swift 3中有一个快速循环,返回国家代码的完整列表

let countryCode = NSLocale.isoCountryCodes
    for country in countryCode {
        print(country)
    }
@抢劫


使用这些代码,您将获得您所在地区的国家/地区代码,如果您没有获得“静止”,则只需在“电话设置”->“常规”->“语言和地区”中进行更改,然后根据地区设置设置您想要的地区

Swift 4.0代码以获取国家/地区名称:

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

打印:可选(“NG”)可选(“尼日利亚”)//对于尼日利亚地区设置,@Denis
Locale
提到的尼日利亚地区设置仅仅是关于当前使用的地区设置的设置,它并不意味着您所在的实际国家

但是,建议使用
CLLocationManager
获取当前位置&
CLGeocoder
执行反向地理编码,这意味着提示用户访问位置服务

从移动运营商那里获取国家代码怎么样

import CoreTelephony

guard carrier = CTTelephonyNetworkInfo().subscriberCellularProvider else {
    //iPad
    return
}

let countryST = carrier.isoCountryCode!

对于iOS4,你有什么解决方案吗?对于iOS4,你可以得到你当前的坐标lat&long,并使用一些谷歌web服务来反向地理编码
    if let countryCode = Locale.current.regionCode {
        let country =   Locale.current.localizedString(forRegionCode: countryCode)
    }