iOS应用部分权限控制-创新互联
整理下iOS开发中常用的权限控制,只整理里一些常用的并不全。
创新互联-专业网站定制、快速模板网站建设、高性价比平湖网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式平湖网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖平湖地区。费用合理售后完善,十年实体公司更值得信赖。#importtypedef 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 用于访问提醒
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享题目:iOS应用部分权限控制-创新互联
文章源于:http://ybzwz.com/article/pshdo.html