189 8069 5689

iOS应用部分权限控制

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

创新互联是一家专业提供永胜企业网站建设,专注与成都做网站、成都网站制作、成都外贸网站建设HTML5、小程序制作等业务。10年已为永胜众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

#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    用于访问提醒


本文名称:iOS应用部分权限控制
网页网址:http://cdxtjz.com/article/jdshce.html

其他资讯