应用CLLocationManager 的两个方法
-
[CLLocationManagerlocationServicesEnabled] 判断设备是否开启定位功能
-
[CLLocationManagerauthorizationStatus] 判断设备是否允许本程序的定位服务
- (BOOL)isOnLocation { BOOL isOn = false; if (([CLLocationManager locationServicesEnabled]) && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) { NSLog(@"定位已经开启"); isOn = true; } else { NSLog(@"定位未开启"); isOn = false; } return isOn; }
使用:
if (![self isOnLocation]) { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"您的定位服务没有打开,请在设置中开启" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alertView show]; //[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"prefs:root=LOCATION_SERVICES"]]; }
当服务关闭时: