iOS~判斷應用是否有定位權限


在特定場景下我們需要判斷用戶是否允許應用獲取定位權限


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) { //定位不能用 }

有不足之處還望補充。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM