1. 程式人生 > >iOS相機、麥克風等許可權的判斷與設定

iOS相機、麥克風等許可權的判斷與設定

一、iOS應用許可權檢測

在涉及到這個問題的時候,首先為了適配iOS10系統,我們必須首先在info.plist檔案中宣告將要用到的許可權,否則將會引起崩潰如下:
“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.”
那麼設定許可權宣告的的方式如下:
螢幕快照 2017-01-09 下午7.52.07.png


我們需要點選Info.plist中加號,增加需要授權key值並填寫相應的許可權使用宣告。

1.相機與麥克風

檢測相機與麥克風許可權需要匯入AVFoundataion框架

#import <AVFoundation/AVFoundation.h>

 /**
//相機、麥克風的授權狀態
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,//未詢問過使用者是否授權
AVAuthorizationStatusRestricted, //未授權,例如家長控制
AVAuthorizationStatusDenied, //未授權,使用者曾選擇過拒絕授權
AVAuthorizationStatusAuthorized //已經授權
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
*/
/**
 檢測相機的方法
 @param permissionGranted 相機授權成功執行的方法
 @param noPermission 相機授權失敗或者未授權執行的方法
 */
+ (void)checkCameraAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
    AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch
(videoAuthStatus) { case AVAuthorizationStatusNotDetermined: { //第一次提示使用者授權 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { granted ? permissionGranted() : noPermission(); }]; break; } case AVAuthorizationStatusAuthorized: { //通過授權 permissionGranted(); break; } case AVAuthorizationStatusRestricted: //不能授權 NSLog(@"不能完成授權,可能開啟了訪問限制"); case AVAuthorizationStatusDenied:{ //提示跳轉到相機設定(這裡使用了blockits的彈窗方法) UIAlertView *alert = [UIAlertView bk_showAlertViewWithTitle:@"相機授權" message:@"跳轉相機授權設定" cancelButtonTitle:@"取消" otherButtonTitles:@[@"設定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex == 1) { //請求授權 [self requetSettingForVideo]; } }]; [alert show]; } break; default: break; } }

2.相簿

這裡針對於iOS8及其以後的系統相簿檢測方法,使用到的PHPhotoLibrary需要匯入Photos框架。

#import <Photos/Photos.h>
//相簿的授權狀態
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
    PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
    PHAuthorizationStatusRestricted,        // 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.
    PHAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    PHAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);

/**
 檢測訪問相簿的許可權
 這裡的方法適用於iOS8及其以後版本
 @param permissionGranted 相簿授權成功執行的方法
 @param noPermission 相簿授權失敗或者未授權執行的方法
 */
+ (void)checkPhotoAlbumAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
    PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
    switch (photoAuthStatus) {
        case PHAuthorizationStatusNotDetermined:
        {
            //第一次提示使用者授權
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                status == PHAuthorizationStatusAuthorized ? permissionGranted() : noPermission();
            }];
            break;
        }
        case PHAuthorizationStatusAuthorized:
        {
            //已經通過授權
            permissionGranted();
            break;
        }
        case PHAuthorizationStatusRestricted:
            //不能授權
            NSLog(@"不能完成授權,可能開啟了訪問限制");
        case PHAuthorizationStatusDenied:{
            //提示跳轉相簿授權設定
            UIAlertView *alert = [UIAlertView  bk_showAlertViewWithTitle:@"相簿授權" message:@"跳轉相簿授權設定" cancelButtonTitle:@"取消" otherButtonTitles:@[@"設定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex == 1) {
                    [self requetSettingForPhotoAlbum];
                }
            }];
            [alert show];
            break;
        }
        default:
            break;
    }
}

二、iOS應用跳轉許可權設定

在iOS8以後的系統中,跳轉設定使用如下方法:

+ (void)requetSettingForAuth{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([ [UIApplication sharedApplication] canOpenURL:url])
    {   
        [[UIApplication sharedApplication] openURL:url];
    }
}