定位,是现在项目中经常遇到的需求,而对于iOS,更是经常会遇到需求说判断用户是否开启定位,从而来进行下一步逻辑,否则很可能影响用户体验甚至导致app go die。所以,判断iPhone是否已经开启定位功能使用并且有必要。
/**
- 总定位服务是否开始
- @return
*/
- (BOOL)isLocationServiceOn;
/**
- 当前app服务定位是否开启
- @return
*/
- (BOOL)isCurrentAppLocatonServiceOn;
/**
- 当前app服务定位是否已决定
- @return
*/
(BOOL)isLocationServiceDetermined;
(BOOL)isLocationServiceOn
{
return [CLLocationManager locationServicesEnabled];
}(BOOL)isCurrentAppLocatonServiceOn
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
return NO;
} else {
return YES;
}
}(BOOL)isLocationServiceDetermined
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (kCLAuthorizationStatusNotDetermined == status) {
return NO;
} else {
return YES;
}
}
比如 我要判断用户是否允许app定位
if (![self isCurrentAppLocatonServiceOn]) {
//没有开启
todosomething...
}else {
//已经开启
todosomething...
}