判断位置逻辑,参考博客:http://blog.csdn.net/cuibo1123/article/details/45691631 感谢!
公司产品因为部分功能只能在国内地区上线,所以需要增加:判断用户所在地区是否在国内。
iOS10以后,首先需要在plist中添加权限key值:Privacy - Location Always Usage Description
和Privacy - Location When In Use Usage Description
code:
- (void)checkLocation {
if ([CLLocationManager locationServicesEnabled]) {
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
_locationManager.distanceFilter = 10.0f;
[_locationManager requestAlwaysAuthorization];
[_locationManager startUpdatingLocation];
} else {
NSLog(@"用户不允许启动定位");
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{
[_locationManager stopUpdatingHeading];
CLLocation *newLocation = locations.lastObject;
if (![[ZCChinaLocation shared] isInsideChina: newLocation.coordinate]) {
//不在国内
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//定位失败
}