iOS相关权限检测和申请
2018-08-06 本文已影响0人
海上飞鸟
iOS权限相关的检测和申请
在iOS开发过程中常用到的权限整理如下:
- 相册权限检测
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status) {
case PHAuthorizationStatusNotDetermined:
NSLog(@"用户还未做出任何选择");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"权限收到限制,可能是家长控制权限");
break;
case PHAuthorizationStatusDenied:
NSLog(@"没有授权");
break;
case PHAuthorizationStatusAuthorized:
NSLog(@"已经授权");
break;
default:
break;
}
相册权限申请
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"同意授权");
}else{
NSLog(@"未同意或未选择");
}
}];
- 相机权限检测
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusAuthorized) {
NSLog(@"有权限");
}else{
NSLog(@"没有");
}
相机权限申请
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
NSLog(@"同意");
}
}];
-
麦克风
把上面的AVMediaTypeVideo换成AVMediaTypeAudio即可 -
网络权限
需要进入头文件#import <CoreTelephony/CTCellularData.h
CTCellularData *cellularData = [CTCellularData new];
CTCellularDataRestrictedState state = [cellularData restrictedState];
if (state == kCTCellularDataRestricted) {
NSLog(@"kCTCellularDataRestricted");
}else if (state == kCTCellularDataNotRestricted){
NSLog(@"kCTCellularDataNotRestricted");
}else{
NSLog(@"kCTCellularDataRestrictedStateUnknown");
}
- 推送权限检测
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
switch (setting.types) {
case UIUserNotificationTypeNone:
NSLog(@"在收到通知后,应用程序可能不呈现任何UI。");
break;
case UIUserNotificationTypeBadge:
NSLog(@"应用程序可以在收到通知时标记其图标。");
break;
case UIUserNotificationTypeSound:
NSLog(@"应用程序可以在接收到通知时播放声音。");
break;
case UIUserNotificationTypeAlert:
NSLog(@"应用程序可以在接收到通知时显示警报。");
break;
default:
break;
}
推送权限申请
UIUserNotificationSettings *requeatSet = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:requeatSet];
- 通讯录权限检测
//ios9之前,检测
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusAuthorized) {
NSLog(@"有权限");
}else{
NSLog(@"没有");
}
//iOS9及以上,检测
CNAuthorizationStatus statu = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (statu == CNAuthorizationStatusAuthorized) {
NSLog(@"有权限");
}else{
NSLog(@"没有");
}
通讯录权限申请
//ios9之前,申请
ABAddressBookRef ref = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"同意");
}
});
//iOS9及以上,申请
CNContactStore *store = [CNContactStore new];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"同意");
}
}];
- 定位权限检测
引入头文件#import <CoreLocation/CoreLocation.h>
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
switch (status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
break;
default:
break;
}
定位权限申请
CLLocationManager *manager = [CLLocationManager new];
[manager requestAlwaysAuthorization];
[manager requestWhenInUseAuthorization];
manager.delegate = self;
//代理方法中判断用户的选择
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
break;
default:
break;
}
}