您好,登錄后才能下訂單哦!
整理下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 用于訪問提醒
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。