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) {
定位不能用
iOS 判斷應用是否有使用相機的許可權NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied){
NSLog(@"相機許可權受限");
return;
}
-------
全部狀態
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, User has not yet made a choice with regards to this application
ALAuthorizationStatusRestricted, This application is not authorized to aess photo data.
The user cannot change this application’s status, possibly due to active restrictions
such as parental controls being in place.
ALAuthorizationStatusDenied, User has explicitly denied this application aess to photos data.
ALAuthorizationStatusAuthorized User has authorized this application to aess photos data.
} __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
註意:要新增 AVFoundation 庫。
設定-隱私-相機
可以設定單個APP訪問相機的許可權
第壹次用APP使用相機時也都會有提示“是否允許使用相機”,不論妳選允許或不允許APP都出現在上面的許可權列表裏
設定--隱私--相機,裏面設定各種應用能否使用相機的許可權。
歡迎采納,謝謝~
android判斷應用是否有某個許可權可以使用如下方式來判斷
PackageManager pm = getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED ==
pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));
if (permission) {
showToast("有這個許可權");
}else {
showToast("木有這個許可權");
}
這個還是比較容易的,首先妳通過PackageManager獲取某個程式的包名獲得這個程式的PackageInfo(程式資訊),然後獲得這個程式的所有許可權列表,壹個個匹對壹下。希望對妳有幫助。