iOS 相机 麦克风 照片 权限的检测与获取

获取相机、相册、麦克风、定位等权限,需要提前在info.plist添加获取权限声明,否则会崩溃,提示信息如下:

[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.

一、相机
首先引入系统库

#import 

(1)检测相机权限
首先我们要先了解AVAuthorizationStatus各个枚举的含义

typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
    // 用户还没有关于这个应用程序做出了选择
    ALAuthorizationStatusNotDetermined NS_ENUM_DEPRECATED_IOS(6_0, 9_0) = 0, // User has not yet made a choice with regards to this application
    // 这个应用程序未被授权访问图片数据。用户不能更改该应用程序的状态,可能是由于活动的限制,如家长控制到位。
    ALAuthorizationStatusRestricted NS_ENUM_DEPRECATED_IOS(6_0, 9_0),        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //  such as parental controls being in place.
    // 用户已经明确否认了这个应用程序访问图片数据
    ALAuthorizationStatusDenied NS_ENUM_DEPRECATED_IOS(6_0, 9_0),            // User has explicitly denied this application access to photos data.
    // 用户授权此应用程序访问图片数据
    ALAuthorizationStatusAuthorized NS_ENUM_DEPRECATED_IOS(6_0, 9_0)        // User has authorized this application to access photos data.
} NS_DEPRECATED_IOS(6_0, 9_0, "Use PHAuthorizationStatus in the Photos framework instead");
- (void) checkVideoStatus
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
        //没有询问是否开启相机
        self.videoStatus = @"AVAuthorizationStatusNotDetermined";
        break;
        case AVAuthorizationStatusRestricted:
        //未授权,家长限制
        self.videoStatus = @"AVAuthorizationStatusRestricted";
        break;
        case AVAuthorizationStatusDenied:
        //未授权
        self.videoStatus = @"AVAuthorizationStatusDenied";
        break;
        case AVAuthorizationStatusAuthorized:
        //玩家授权
        self.videoStatus = @"AVAuthorizationStatusAuthorized";
        break;
        default:
        break;
    }

(2)获取相机权限---弹出弹框

 //授权相机
- (void)videoAuthAction
{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        NSLog(@"%@",granted ? @"相机准许":@"相机不准许");
    }];
}

二、相册
首先引入系统库

#import 

(1)检测相册权限

//检查照片权限
- (void) checkPhotoStauts{
    PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
    switch (photoAuthorStatus) {
        case PHAuthorizationStatusAuthorized:
        self.photoLibraryStatus = @"PHAuthorizationStatusAuthorized";
        break;
        case PHAuthorizationStatusDenied:
        self.photoLibraryStatus = @"PHAuthorizationStatusDenied";
        break;
        case PHAuthorizationStatusNotDetermined:
        self.photoLibraryStatus = @"PHAuthorizationStatusNotDetermined";
        break;
        case PHAuthorizationStatusRestricted:
        self.photoLibraryStatus = @"PHAuthorizationStatusRestricted";
        break;
        default:
        break;
    }
   }

(2)获取相册权限---弹出弹框

//授权照片
- (void)phontLibraryAction{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    }];
}

三、麦克风
首先引入系统库

#import 

(1)检测麦克风权限

- (void) checkAudioStatus{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
        //没有询问是否开启麦克风
        self.audioStatus = @"AVAuthorizationStatusNotDetermined";
        break;
        case AVAuthorizationStatusRestricted:
        //未授权,家长限制
        self.audioStatus = @"AVAuthorizationStatusRestricted";
        break;
        case AVAuthorizationStatusDenied:
        //玩家未授权
        self.audioStatus = @"AVAuthorizationStatusDenied";
        break;
        case AVAuthorizationStatusAuthorized:
        //玩家授权
        self.audioStatus = @"AVAuthorizationStatusAuthorized";
        break;
        default:
        break;
    }
}

(2)获取麦克风权限----弹出弹框

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
           NSLog(@"%@",granted ? @"麦克风准许":@"麦克风不准许");
       }];

四、定位---
首先引入系统库

#import 

(1)获取定位权限

CLAuthorizationStatus status = [CLLocationManager authorizationStatus]

下面是CLAuthorizationStatus各个值的含义

授权状态为枚举值:
kCLAuthorizationStatusNotDetermined                  //用户尚未对该应用程序作出选择
kCLAuthorizationStatusRestricted                     //应用程序的定位权限被限制 
kCLAuthorizationStatusAuthorizedAlways               //一直允许获取定位
kCLAuthorizationStatusAuthorizedWhenInUse            //在使用时允许获取定位
kCLAuthorizationStatusAuthorized                     //已废弃,相当于一直允许获取定位
kCLAuthorizationStatusDenied                         //拒绝获取定位

(2)使用高德地图时,遇到了不弹出定位弹框的情况,原因是没有添加下面的两行代码导致
当前项目使用的是高德地图,高德地图更新后,需要设置这两行代码,否则,不会弹出获取权限弹框

 [AMapLocationManager updatePrivacyAgree:AMapPrivacyAgreeStatusDidAgree];
    [AMapLocationManager updatePrivacyShow:AMapPrivacyShowStatusDidShow privacyInfo:AMapPrivacyInfoStatusDidContain]

你可能感兴趣的:(iOS 相机 麦克风 照片 权限的检测与获取)