如何在某个程序里面判定当前应用程序的定位服务是否可用
SO上有一个答案。
[CLLocationManager locationServicesEnabled]
检测的是整个iOS系统的位置服务开关,无法检测当前应用是否被关闭,只能通过CLLocationManagerDelegate
的locationManager:didFailWithError:
方法去检测:
- (void)locationManager: (CLLocationManager *)manager
didFailWithError: (NSError *)error {
NSString *errorString;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
//Access denied by user
errorString = @"Access to Location Services denied by user";
//Do something...
break;
case kCLErrorLocationUnknown:
//Probably temporary...
errorString = @"Location data unavailable";
//Do something else...
break;
default:
errorString = @"An unknown error has occurred";
break;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
具体的CLError信息可以看CLLocationManager文档
2013 年的答案太老了, iOS 4.2 以后可以用 +(BOOL)locationServicesEnabled 和 + (CLAuthorizationStatus)authorizationStatus 两个方法来检查,前者是检查手机是否开启了定位,后者是检查应用是否拥有定位的权限。
正文完