您好,登錄后才能下訂單哦!
整理下iOS開發(fā)中常用的權(quán)限控制,只整理里一些常用的并不全。
#import <Foundation/Foundation.h> typedef void (^AuthorizedFinishBlock)(); @interface LYAuthorizedMaster : NSObject #pragma mark - 攝像頭權(quán)限 +(BOOL)checkCameraAuthority; +(void)cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 麥克風(fēng)權(quán)限 +(BOOL)checkAudioAuthority; +(void)audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 相冊權(quán)限 +(BOOL)checkAlbumAuthority; +(void)albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 推送通知權(quán)限 +(BOOL)checkPushNotificationAuthority; +(void)pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 推送通知權(quán)限 +(BOOL)checkLocationAuthority; +(void)locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail; #pragma mark - 通訊錄權(quán)限 +(BOOL)checkAddressBookAuthority; +(void)AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;
下面是.m文件
里面引入了很多庫文件,也不是所用項目都會用到的,用不到的注掉就好。
#import "LYAuthorizedMaster.h" #import <AVFoundation/AVFoundation.h> //攝像頭麥克風(fēng) 必須 #import <AssetsLibrary/AssetsLibrary.h> //相冊權(quán)限 #import <CoreLocation/CoreLocation.h> //位置權(quán)限 #import <AddressBook/AddressBook.h> //通訊錄權(quán)限 #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:@"沒有權(quán)限" message:[NSString stringWithFormat:@"請開啟‘%@’對 %@ 的使用權(quán)限",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 - 攝像頭權(quán)限 +(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:@"相機(jī)"]; } } #pragma mark - 麥克風(fēng)權(quán)限 +(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:@"麥克風(fēng)"]; } } #pragma mark - 相冊權(quán)限 +(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 - 位置權(quán)限 +(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 - 推送通知權(quán)限 +(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 - 通訊錄權(quán)限 +(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:@"通訊錄"]; } }
最后有些時會遇到不彈出權(quán)限提示,或需要在提示框增加詳細(xì)描述的時候,需要手動在info.plist加一些字段。
NSLocationWhenInUseUsageDescription 位置權(quán)限 使用期間 狀態(tài)
NSLocationAlwaysUsageDescription 位置權(quán)限 始終 狀態(tài)
下面這些我并沒有都試,所以也不知道是否正確....
NSLocationUsageDescription 用于訪問位置權(quán)限
NSCalendarsUsageDescription 用于訪問日歷權(quán)限
NSContactsUsageDescription 用于訪問聯(lián)絡(luò)人
NSPhotoLibraryUsageDescription 用于訪問相冊
NSRemindersUsageDescription 用于訪問提醒
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。