iOS应用部分权限控制

发布时间:2020-07-23 02:17:30 作者:liyun2422
来源:网络 阅读:516

整理下iOS开发中常用的权限控制,只整理里一些常用的并不全。

#import <Foundation/Foundation.h>

typedef void (^AuthorizedFinishBlock)();

@interface LYAuthorizedMaster : NSObject


#pragma mark - 摄像头权限
+(BOOL)checkCameraAuthority;
+(void)cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 麦克风权限
+(BOOL)checkAudioAuthority;
+(void)audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 相册权限
+(BOOL)checkAlbumAuthority;
+(void)albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 推送通知权限
+(BOOL)checkPushNotificationAuthority;
+(void)pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 推送通知权限
+(BOOL)checkLocationAuthority;
+(void)locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 通讯录权限
+(BOOL)checkAddressBookAuthority;
+(void)AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;


下面是.m文件

里面引入了很多库文件,也不是所用项目都会用到的,用不到的注掉就好。

#import "LYAuthorizedMaster.h"
#import <AVFoundation/AVFoundation.h>       //摄像头麦克风 必须
#import <AssetsLibrary/AssetsLibrary.h>     //相册权限
#import <CoreLocation/CoreLocation.h>       //位置权限
#import <AddressBook/AddressBook.h>         //通讯录权限

#import "AppDelegate.h"

#define kAPPName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]

@implementation LYAuthorizedMaster

#pragma mark -
+(BOOL)checkAuthority:(AVAuthorizationStatus)_status{
    return (_status == AVAuthorizationStatusAuthorized) || (_status == AVAuthorizationStatusNotDetermined);
}
+(void)showAlertController:(AuthorizedFinishBlock)_block device:(NSString *)_device{
    UIAlertController *_alertC = [UIAlertController alertControllerWithTitle:@"没有权限" message:[NSString stringWithFormat:@"请开启‘%@’对 %@ 的使用权限",kAPPName,_device] preferredStyle:UIAlertControllerStyleAlert];
    [_alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [_alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }]];
    [((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:_alertC animated:YES completion:_block];
}

#pragma mark - 摄像头权限
+(BOOL)checkCameraAuthority{
    return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]];
}
+(void)cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;{
    if ([self checkCameraAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"相机"];
    }
}

#pragma mark - 麦克风权限
+(BOOL)checkAudioAuthority{
    return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]];
}
+(void)audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    if ([self checkAudioAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"麦克风"];
    }
}

#pragma mark - 相册权限
+(BOOL)checkAlbumAuthority{
    return [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;
}
+(void)albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;
{
    if ([self checkAlbumAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"照片"];
    }
}

#pragma mark - 位置权限
+(BOOL)checkLocationAuthority {
    return [CLLocationManager locationServicesEnabled];
}
+(void)locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkLocationAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"位置"];
    }
}

#pragma mark - 推送通知权限
+(BOOL)checkPushNotificationAuthority {
    return [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone;
}
+(void)pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkAlbumAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"通知"];
    }
}

#pragma mark - 通讯录权限
+(BOOL)checkAddressBookAuthority {
    return ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized || ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined;
}
+(void)AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkAddressBookAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"通讯录"];
    }
}


最后有些时会遇到不弹出权限提示,或需要在提示框增加详细描述的时候,需要手动在info.plist加一些字段。


NSLocationWhenInUseUsageDescription    位置权限 使用期间 状态

NSLocationAlwaysUsageDescription    位置权限 始终 状态


下面这些我并没有都试,所以也不知道是否正确....

NSLocationUsageDescription    用于访问位置权限

NSCalendarsUsageDescription    用于访问日历权限

NSContactsUsageDescription    用于访问联络人

NSPhotoLibraryUsageDescription    用于访问相册

NSRemindersUsageDescription    用于访问提醒


推荐阅读:
  1. 在HBuilderX平台打包iOS应用介绍
  2. 窗体按钮权限控制

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

摄像头 ios 相册权限

上一篇:umask-创建文件或目录的默认权限

下一篇:C/C++编译器教程之如何安装vs,看了的人应该都会装,只要不手残

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》