參考http://mt.sohu.com/20160707/n458265498.shtml
對定位權(quán)限、定位是否開啟的判斷
// ios8.0+需要請求授權(quán)
if (isIOS(8.0)) {
// 要在此處, 請求授權(quán), 但是請求哪個權(quán)限, 沒法確定, 靠其他開發(fā)者確定;
// 1. 獲取info.plist 文件內(nèi)容
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
// NSLog(@"%@", infoDic);
// 2. 獲取其他開發(fā)人員, 填寫的key
NSString *always = infoDic[@"NSLocationAlwaysUsageDescription"];
NSString *whenInUse = infoDic[@"NSLocationWhenInUseUsageDescription"];
if ([always length] > 0) {
[_locationM requestAlwaysAuthorization];
} else if ([whenInUse length] > 0) {
[_locationM requestWhenInUseAuthorization];
// 在前臺定位授權(quán)狀態(tài)下, 必須勾選后臺模式location udpates才能獲取用戶位置信息
NSArray *services = infoDic[@"UIBackgroundModes"];
if (![services containsObject:@"location"]) {
DBLog(@"友情提示: 當(dāng)前狀態(tài)是前臺定位授權(quán)狀態(tài), 如果想要在后臺獲取用戶位置信息, 必須勾選后臺模式 location updates");
} else {
if (isIOS(9.0)) {
_locationM.allowsBackgroundLocationUpdates = YES;
}
}
} else {
DBLog(@"錯誤---如果在iOS8.0之后定位, 必須在info.plist, 配置NSLocationWhenInUseUsageDescription 或者 NSLocationAlwaysUsageDescription");
}
}
// 判斷是否授權(quán)
CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
if (authStatus == kCLAuthorizationStatusRestricted || authStatus == kCLAuthorizationStatusDenied) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"沒有位置權(quán)限" message:@"請去設(shè)置-隱私-定位服務(wù)中對52度授權(quán)" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"現(xiàn)在就去" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"以后再說" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
}
// 定位服務(wù)未開啟
if (![CLLocationManager locationServicesEnabled]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"打開“定位服務(wù)”來允許“52度”確定您的位置" message:@"52度將獲取您的位置,為您提供更精確的服務(wù)" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"設(shè)置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}