

聯(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
來源:知乎