本文共 5950 字,大约阅读时间需要 19 分钟。
整理下iOS开发中 应用申请常用权限
#import 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 //摄像头麦克风 必须#import //相册权限#import //位置权限#import //通讯录权限#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 用于访问提醒
数据 | 系统授权支持 |
位置信息 | 位置信息的授权状态由CLLocationManager类中的authorizationStatus方法来支持。在Info.plist文件中添加NSLocationUsageDescription关键字的描述来体现使用说明。 |
照片 | 照片的授权状态是由ALAssetsLibrary类中的authorizationStatus方法来支持。在Info.plist文件中添加关键字NSPhotoLibraryUsageDescription 的描述来体现 使用说明 |
音乐,视频及其它多媒体资源 | 由ALAssetsLibrary类的authorizationStatus方法来提供支持。 |
联系人 | 由ABAddressBookGetAuthorizationStatus函数提供支持。在Info.plist文件中添加NSContactsUsageDescription关键字的描述来体现使用说明 |
日历数据 | 由EKEventStore的authorizationStatusForEntityType:方法来提供支持。在Info.plist文件中添加NSCalendarsUsageDescription关键字的描述来体现使用说明 |
消息提醒 | 由EKEventStore的authorizationStatusForEntityType:方法来提供支持。在Info.plist文件中添加NSRemindersUsageDescription关键字的描述来体现使用说明 |
蓝牙设备 | 由CBCentralManager的state属性来提供支持。在Info.plist文件中添加NSBluetoothPeripheralUsageDescription关键字的描述来体现使用说明 |
转载地址:http://cnmhx.baihongyu.com/