整理下iOS開(kāi)發(fā)中常用的權(quán)限控制,只整理里一些常用的并不全。
創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供永勝企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、HTML5、小程序制作等業(yè)務(wù)。10年已為永勝眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jì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 - 相冊(cè)權(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文件
里面引入了很多庫(kù)文件,也不是所用項(xiàng)目都會(huì)用到的,用不到的注掉就好。
#import "LYAuthorizedMaster.h" #import <AVFoundation/AVFoundation.h> //攝像頭麥克風(fēng) 必須 #import <AssetsLibrary/AssetsLibrary.h> //相冊(cè)權(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:@"沒(méi)有權(quán)限" message:[NSString stringWithFormat:@"請(qǐng)開(kāi)啟‘%@’對(duì) %@ 的使用權(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 - 相冊(cè)權(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:@"通訊錄"]; } }
最后有些時(shí)會(huì)遇到不彈出權(quán)限提示,或需要在提示框增加詳細(xì)描述的時(shí)候,需要手動(dòng)在info.plist加一些字段。
NSLocationWhenInUseUsageDescription 位置權(quán)限 使用期間 狀態(tài)
NSLocationAlwaysUsageDescription 位置權(quán)限 始終 狀態(tài)
下面這些我并沒(méi)有都試,所以也不知道是否正確....
NSLocationUsageDescription 用于訪(fǎng)問(wèn)位置權(quán)限
NSCalendarsUsageDescription 用于訪(fǎng)問(wèn)日歷權(quán)限
NSContactsUsageDescription 用于訪(fǎng)問(wèn)聯(lián)絡(luò)人
NSPhotoLibraryUsageDescription 用于訪(fǎng)問(wèn)相冊(cè)
NSRemindersUsageDescription 用于訪(fǎng)問(wèn)提醒
網(wǎng)站欄目:iOS應(yīng)用部分權(quán)限控制
鏈接分享:http://m.2m8n56k.cn/article24/jdshce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、軟件開(kāi)發(fā)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站營(yíng)銷(xiāo)、小程序開(kāi)發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)