在特定場景下我們需要判斷用戶是否允許應用獲取定位權限
1.導入類庫:
#import <CoreLocation/CLLocationManager.h>
2.判斷用戶手機是否開啟了定位服務:
這里就要查看CLLocationManager的授權狀態,此方法會返回當前授權狀態: [CLLocationManager authorizationStatus] 授權狀態為枚舉值: kCLAuthorizationStatusNotDetermined //用戶尚未對該應用程序作出選擇 kCLAuthorizationStatusRestricted //應用程序的定位權限被限制 kCLAuthorizationStatusAuthorizedAlways //一直允許獲取定位 kCLAuthorizationStatusAuthorizedWhenInUse //在使用時允許獲取定位 kCLAuthorizationStatusAuthorized //已廢棄,相當於一直允許獲取定位 kCLAuthorizationStatusDenied //拒絕獲取定位
3.判斷用戶是否授權應用獲取定位權限的完整代碼:
if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) { //定位功能可用 }else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) { //定位不能用 }
有不足之處還望補充。