ios 權(quán)限知多少


聯(lián)網(wǎng)權(quán)限

#import ?<CoreTelephony/CTCarrier.h>

#import <CoreTelephony/CTCellularData.h>

應(yīng)用啟動后,檢測應(yīng)用中是否有聯(lián)網(wǎng)權(quán)限

CTCellularData *cellularData = [[CTCellularData alloc]init];

cellularData.cellularDataRestrictionDidUpdateNotifier =? ^(CTCellularDataRestrictedState state){?

?//獲取聯(lián)網(wǎng)狀態(tài)?

?switch (state) {? ? ? ??

case kCTCellularDataRestricted:? ? ? ? ? ? ? ? ?

?NSLog(@"Restricrted");? ? ? ?

dispatch_async(dispatch_get_main_queue(), ^{? ? ? ? ? ? ? ? ?

UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"已為“XXX”關(guān)閉蜂窩移動數(shù)據(jù)"

message:@"您可以在系統(tǒng)“設(shè)置”中為此應(yīng)用打開蜂窩移動數(shù)據(jù)。"

delegate:nil

cancelButtonTitle:@"確定"

otherButtonTitles:nil];

[alertshow];

[alertrelease];

});

?? break;? ? ? ?

?case kCTCellularDataNotRestricted:? ? ? ? ? ? ? ??

? NSLog(@"Not Restricted");? ? ? ? ? ? ? ? ? ? ? ? ? ?

?break;? ? ? ?

?case kCTCellularDataRestrictedStateUnknown:? ? ? ? ? ? ? ??

? NSLog(@"Unknown");? ? ? ? ? ? ? ? ? ? ? ? ? ??

break;? ? ??

? default:? ? ? ? ? ?

?break;? };};

查詢應(yīng)用是否有聯(lián)網(wǎng)功能

CTCellularData *cellularData = [[CTCellularData alloc]init];

CTCellularDataRestrictedState state = cellularData.restrictedState;?

switch (state) {??

?case kCTCellularDataRestricted:? ? ? ??

?NSLog(@"Restricrted");? ? ? ? ? ??

? break;??

?case kCTCellularDataNotRestricted:? ? ? ?

?NSLog(@"Not Restricted");? ? ? ? ? ??

? break; ? ?

?case kCTCellularDataRestrictedStateUnknown: ? ? ??

?NSLog(@"Unknown");? ? ? ? ? ? ?

?break;?

?default:? ? ? ?

?break;}

相冊權(quán)限--iOS 9.0之前

導入頭文件@import AssetsLibrary;

檢查是否有相冊權(quán)限

PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];

switch (photoAuthorStatus) {?

?case PHAuthorizationStatusAuthorized:? ? ? ?

?NSLog(@"Authorized");? ? ? ? ? ? ?

?break;?

?case PHAuthorizationStatusDenied:? ? ??

? NSLog(@"Denied");? ? ? ? ? ? ?

?break;? case PHAuthorizationStatusNotDetermined:? ? ? ?

?NSLog(@"not Determined");? ? ? ? ? ??

? break;? ?

?case PHAuthorizationStatusRestricted:? ? ??

? NSLog(@"Restricted");? ? ? ? ? ? ?

?break;? ?

?default:? ? ? ? ? ? ?

?break;}

獲取相冊權(quán)限

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

? if (status == PHAuthorizationStatusAuthorized) {? ? ? ??

NSLog(@"Authorized");?

?}else{? ? ? ??

NSLog(@"Denied or Restricted");?

?}? }];

相機和麥克風權(quán)限

導入頭文件@import AVFoundation;

檢查是否有相機或麥克風權(quán)限

AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相機權(quán)限

AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麥克風權(quán)限

switch (AVstatus) {??

case AVAuthorizationStatusAuthorized:? ? ? ??

NSLog(@"Authorized");? ? ? ? ? ? ?

?break;? ?

?case AVAuthorizationStatusDenied:? ? ? ?

?NSLog(@"Denied");? ? ? ? ? ? ?

?break;? ??

case AVAuthorizationStatusNotDetermined:? ? ? ??

NSLog(@"not Determined");? ? ? ? ? ??

? break;? case AVAuthorizationStatusRestricted:? ? ? ?

?NSLog(@"Restricted");? ? ? ? ? ? ?

?break;? ??

default:? ? ? ? ? ??

? break;}

獲取相機或麥克風權(quán)限

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相機權(quán)限?

?if (granted) {? ? ?

?NSLog(@"Authorized");?

?}else{? ??

? NSLog(@"Denied or Restricted");??

}}];

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麥克風權(quán)限??

if (granted) {? ??

? NSLog(@"Authorized");?

?}else{? ??

? NSLog(@"Denied or Restricted");?

?}}];

定位權(quán)限

導入頭文件@import CoreLocation;

由于iOS8.0之后定位方法的改變,需要在info.plist中進行配置;

檢查是否有定位權(quán)限

BOOL isLocation = [CLLocationManager locationServicesEnabled];

if (!isLocation) {?

?NSLog(@"not turn on the location");

}

CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];

switch (CLstatus) {??

case kCLAuthorizationStatusAuthorizedAlways: ? ? ??

NSLog(@"Always Authorized");? ? ? ? ? ? ?

?break;??

case kCLAuthorizationStatusAuthorizedWhenInUse:? ? ? ?

?NSLog(@"AuthorizedWhenInUse");? ? ? ? ? ? ?

?break;?

?case kCLAuthorizationStatusDenied:? ? ? ??

NSLog(@"Denied");? ? ? ? ? ?

?? break;? ?

?case kCLAuthorizationStatusNotDetermined:? ? ??

? NSLog(@"not Determined");? ? ? ? ??

? ? break;? ?

?case kCLAuthorizationStatusRestricted:? ? ??

? NSLog(@"Restricted");? ? ? ? ? ? ?

?break;??

? default:? ? ? ? ??

? ? break;}

獲取定位權(quán)限

CLLocationManager *manager = [[CLLocationManager alloc] init];

[manager requestAlwaysAuthorization];//一直獲取定位信息

[manager requestWhenInUseAuthorization];//使用的時候獲取定位信息

在代理方法中查看權(quán)限是否改變

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{?

switch (status) {

? case kCLAuthorizationStatusAuthorizedAlways:? ? ??

? NSLog(@"Always Authorized");? ? ? ? ? ? ?

?break;? ??

case kCLAuthorizationStatusAuthorizedWhenInUse:? ? ? ?

?NSLog(@"AuthorizedWhenInUse");? ? ? ? ? ??

? break;? ?

?case kCLAuthorizationStatusDenied:? ? ? ? ? ? ?

?NSLog(@"Denied");? ? ? ? ? ? ?

?break;? ?

?case kCLAuthorizationStatusNotDetermined:? ? ??

? NSLog(@"not Determined");? ? ? ? ? ? ?

?break;? ?

?case kCLAuthorizationStatusRestricted:? ? ??

? NSLog(@"Restricted");? ? ? ? ? ??

? break;? ??

default:? ? ??

? break;?

?}}

推送權(quán)限

檢查是否有通訊權(quán)限

UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];

switch (settings.types) {?

?case UIUserNotificationTypeNone:? ? ? ?

?NSLog(@"None");? ? ? ? ? ??

? break;? ??

case UIUserNotificationTypeAlert:? ? ??

? NSLog(@"Alert Notification");? ? ? ? ? ??

? break;?

?case UIUserNotificationTypeBadge:? ? ?

?? NSLog(@"Badge Notification");? ? ? ? ? ?

?? break;? ??

case UIUserNotificationTypeSound:? ? ??

? NSLog(@"sound Notification'");? ? ? ? ? ??

? break;??

? default:? ? ? ? ? ? ?

?break;

}

獲取推送權(quán)限

UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

通訊錄權(quán)限

導入頭文件 @import AddressBook;

檢查是否有通訊錄權(quán)限

ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();

switch (ABstatus) {??

?case kABAuthorizationStatusAuthorized:? ? ? ? ? ??

? NSLog(@"Authorized");? ? ? ? ? ??

? break;? ? case kABAuthorizationStatusDenied:? ? ??

? NSLog(@"Denied'");? ? ? ? ? ??

? break;? ??

?case kABAuthorizationStatusNotDetermined:? ? ? ?

?NSLog(@"not Determined");? ? ? ? ? ?

?? break;? ?

?case kABAuthorizationStatusRestricted:? ? ? ?

?NSLog(@"Restricted");? ? ? ? ? ??

? break;? ?

?default:? ? ? ? ? ?

?? break;}

獲取通訊錄權(quán)限

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {?

?if (granted) {? ? ? ?

?NSLog(@"Authorized");? ? ? ?

?CFRelease(addressBook);?

?}else{? ? ? ??

NSLog(@"Denied or Restricted");?

?}

});

日歷、備忘錄權(quán)限

導入頭文件

檢查是否有日歷或者備忘錄權(quán)限

typedef NS_ENUM(NSUInteger, EKEntityType) {

EKEntityTypeEvent,//日歷

EKEntityTypeReminder //備忘

};

EKAuthorizationStatus EKstatus = [EKEventStore? authorizationStatusForEntityType:EKEntityTypeEvent];

switch (EKstatus) {??

case EKAuthorizationStatusAuthorized:? ? ? ?

?NSLog(@"Authorized");? ? ? ? ??

? ? break;??

? case EKAuthorizationStatusDenied:? ? ? ??

NSLog(@"Denied'");? ? ? ? ? ?

?? break;??

? case EKAuthorizationStatusNotDetermined:? ? ? ?

?NSLog(@"not Determined");? ? ? ? ? ??

? break; ??

case EKAuthorizationStatusRestricted:? ? ? ?

?NSLog(@"Restricted");? ? ? ? ? ? ?

?break;??

?default:? ? ??

? break;

}

獲取日歷或備忘錄權(quán)限

EKEventStore *store = [[EKEventStore alloc]init];

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {?

?if (granted) {? ? ? ?

?NSLog(@"Authorized");?

?}else{? ? ? ??

NSLog(@"Denied or Restricted");??

}

}];

最后一點

素有獲取權(quán)限的方法,多用于用戶第一次操作應(yīng)用,iOS 8.0之后,將這些設(shè)置都整合在一起,并且可以開啟或關(guān)閉相應(yīng)的權(quán)限。所有的權(quán)限都可以通過下面的方法打開:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

作者:Larry

鏈接:https://zhuanlan.zhihu.com/p/21526810

來源:知乎

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容