1. 程式人生 > >iOS 相機 麥克風 照片 許可權的檢測與獲取

iOS 相機 麥克風 照片 許可權的檢測與獲取

前期設定
iOS10 中需要在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.

plist中的設定如下圖
這裡寫圖片描述

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 ? @"麥克風准許":@"麥克風不准許");
       }];

3.檢測相機許可權

- (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;
    }
}

4.獲取相機許可權

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

5.檢測照片許可權

//檢查照片許可權
- (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;
    }
   }

6.獲取照片許可權

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