iOS 权限检测

//
//  AuthorityManage.h
//  text
//
//  Created by apple on 2018/2/9.
//  Copyright © 2018年 刘龙飞. All rights reserved.
//

#import 

@interface AuthorityManage : NSObject


///检测麦克风状态
+(void)detectionMicrophoneState:(void(^)(BOOL isAvalible))authorizedBlock;

///检测相册访问权限
+(void)detectionPhotoState:(void(^)(BOOL isAvalible))authorizedBlock;

///检测相机访问权限
+(void)detectionCameraState:(void(^)(BOOL isAvalible))authorizedBlock;

///检测通知权限
+(void)detectionNotificationState:(void(^)(BOOL isAvalible))authorizedBlock;


@end
//
//  AuthorityManage.m
//  text
//
//  Created by apple on 2018/2/9.
//  Copyright © 2018年 刘龙飞. All rights reserved.
//

#import "AuthorityManage.h"
#import 
#import 
#import 

@implementation AuthorityManage

///检测麦克风状态
+(void)detectionMicrophoneState:(void(^)(BOOL isAvalible))authorizedBlock
{
    __block BOOL isAvalible = NO;
    NSString *mediaType = AVMediaTypeAudio;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    //用户尚未授权->申请权限
    if (authStatus == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
            if(granted)
            {
                isAvalible = YES;
                if (authorizedBlock)
                {
                    authorizedBlock(isAvalible);
                }
            }}];
    }
    //用户已经授权
    else if (authStatus == AVAuthorizationStatusAuthorized)
    {
        isAvalible = YES;
        if (authorizedBlock)
        {
            authorizedBlock(isAvalible);
        }
    }
    //用户拒绝授权
    else
    {
        //提示用户开启麦克风权限
        isAvalible = NO;
        if (authorizedBlock)
        {
            authorizedBlock(isAvalible);
        }
    }
}

///检测相册访问权限
+(void)detectionPhotoState:(void(^)(BOOL isAvalible))authorizedBlock
{
    __block BOOL  isAvalible = NO;
    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
    //用户尚未授权
    if (authStatus == PHAuthorizationStatusNotDetermined)
    {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized)
            {
                isAvalible = YES;
                if (authorizedBlock)
                {
                    authorizedBlock(isAvalible);
                }
            }}];
    }
    //用户已经授权
    else if (authStatus == PHAuthorizationStatusAuthorized)
    {
        isAvalible = YES;
        if (authorizedBlock)
        {
            authorizedBlock(isAvalible);
        }
    }
    //用户拒绝授权
    else
    {
        //提示用户开启相册权限
        isAvalible = NO;
        if (authorizedBlock)
        {
            authorizedBlock(isAvalible);
        }
    }
}

///检测相机访问权限
+(void)detectionCameraState:(void(^)(BOOL isAvalible))authorizedBlock
{
    __block BOOL  isAvalible = NO;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    //用户尚未授权
    if (authStatus == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted)
            {
                isAvalible = YES;
                if (authorizedBlock)
                {
                    authorizedBlock(isAvalible);
                }
            }
        }];
    }
    //用户已经授权
    else if (authStatus == AVAuthorizationStatusAuthorized)
    {
        isAvalible = YES;
        if (authorizedBlock)
        {
            authorizedBlock(isAvalible);
        }
    }
    //用户拒绝授权
    else
    {
        //提示用户开启相机权限
        isAvalible = NO;
        if (authorizedBlock)
        {
            authorizedBlock(isAvalible);
        }
    }
}

///检测通知权限
+(void)detectionNotificationState:(void(^)(BOOL isAvalible))authorizedBlock
{
    __block BOOL  isAvalible = NO;
    if ([[[UIDevice currentDevice]systemVersion] floatValue] >= 10) {
        if (@available(iOS 10.0, *)) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        } else {
            // Fallback on earlier versions
        }
        if (@available(iOS 10.0, *)) {
            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
            [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                //用户尚未授权
                if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
                    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
                        if (granted)
                        {
                            isAvalible = YES;
                            if (authorizedBlock)
                            {
                                authorizedBlock(isAvalible);
                            }
                        }
                    }];
                }
                //用户已经授权
                else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized)
                {
                    isAvalible = YES;
                    if (authorizedBlock)
                    {
                        authorizedBlock(isAvalible);
                    }
                }
                //用户拒绝授权
                else
                {
                    isAvalible = NO;
                    if (authorizedBlock)
                    {
                        authorizedBlock(isAvalible);
                    }
                }
            }];
        } else {
            // Fallback on earlier versions
        }
    }
    else
    {
        //用户拒绝授权
        if ([UIApplication sharedApplication].currentUserNotificationSettings.types == UIUserNotificationTypeNone)
        {
            //提示用户开启通知权限
            isAvalible = NO;
            if (authorizedBlock)
            {
                authorizedBlock(isAvalible);
            }
        }
        else
        {
            isAvalible = YES;
            if (authorizedBlock)
            {
                authorizedBlock(isAvalible);
            }
        }
    }
}



@end

  
    ///检测麦克风
    [AuthorityManage detectionMicrophoneState:^(BOOL isAvalible) {
        if (isAvalible == YES) {
            NSLog(@"用户已经授权麦克风");
        }else{
            NSLog(@"没有权限访问您的麦克风,请在“设置-隐私-麦克风”中允许使用");
        }
    }];

    ///检测相册
    [AuthorityManage detectionPhotoState:^(BOOL isAvalible) {
        if (isAvalible == YES) {
            NSLog(@"用户已经授权相册");
        }else{
            NSLog(@"没有权限访问您的相册,请在“设置-隐私-相册”中允许使用");
        }
    }];

    ///检测相机
    [AuthorityManage detectionCameraState:^(BOOL isAvalible) {
        if (isAvalible == YES) {
            NSLog(@"用户已经授权相机");
        }else{
            NSLog(@"没有权限访问您的相机,请在“设置-隐私-相机”中允许使用");
        }
    }];

    ///检测通知
    [AuthorityManage detectionNotificationState:^(BOOL isAvalible) {
        if (isAvalible == YES) {
            NSLog(@"用户已经授权通知");
        }else{
            NSLog(@"没有权限访问您的通知,请在“设置-隐私-通知”中允许使用");
        }
    }];

你可能感兴趣的:(iOS 权限检测)