一、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;
*/
//AVMediaTypeVideo:相機權限
//AVMediaTypeAudio:麥克風權限
/**
檢測相機的方法
@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];
}
}
三、使用注意
我們在檢測授權的時候彈窗會有授權和不授權的回調,有時候我們會在這里處理一些自定義UI問題,這里一定要在主線程中進行,否則會出現崩潰等問題,回到主線程中的操作如下:
dispatch_async(dispatch_get_main_queue(), ^{
//處理UI問題
});