App开发权限

来源:互联网 发布:花哥捏脸数据 编辑:程序博客网 时间:2024/05/16 11:03

一、请求权限的方式

1.打开App时请求权限

2.告知用户授权益处后请求权限

3.在必要的情况下请求权限

4.先展示自定义对话框,同意后再请求权限


二、权限分类

1.联网权限

2.相册权限

3.相机、麦克风权限

4.定位权限

5.推送权限

6.通讯录权限

7.日历、备忘录权限


三、联网权限

引入头文件 import CoreTelephony

CTCellularData *cellularData = [[CTCellularData alloc]init];cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){  //获取联网状态  switch (state) {        case kCTCellularDataRestricted:                  NSLog(@"Restricrted");                            break;        case kCTCellularDataNotRestricted:                  NSLog(@"Not Restricted");                            break;        case kCTCellularDataRestrictedStateUnknown:                  NSLog(@"Unknown");                            break;        default:            break;  };};查询应用是否有联网功能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;}

四、相册权限

导入头文件 import AssetsLibrary

检查是否有相册权限

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;}[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {  if (status == PHAuthorizationStatusAuthorized) {        NSLog(@"Authorized");  }else{        NSLog(@"Denied or Restricted");  }  }];

六、相机和麦克风权限

导入头文件 import AVFoundation

AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相机权限AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限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;}获取相机或麦克风权限[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限  if (granted) {      NSLog(@"Authorized");  }else{      NSLog(@"Denied or Restricted");  }}];[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麦克风权限  if (granted) {      NSLog(@"Authorized");  }else{      NSLog(@"Denied or Restricted");  }}];

七、定位权限

导入头文件 import CoreLocation

ios8.0后需要在info.plist中添加String配置:NSLocationWhenUseUsageDescription\NSLocationAlwaysUsageDesCription

检查是否有定位权限

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;}

获取定位权限

CLLocationManager *manager = [[CLLocationManager alloc] init];[manager requestAlwaysAuthorization];//一直获取定位信息[manager requestWhenInUseAuthorization];//使用的时候获取定位信息

在代理方法中查看权限是否改变

- (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;  }}

八、推送权限

检查是否有通讯权限

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;}

获取推动权限

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

九、通讯录权限

导入头文件 import AddressBook

检查是否有通讯权限

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;}

获取通讯录权限

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {  if (granted) {        NSLog(@"Authorized");        CFRelease(addressBook);  }else{        NSLog(@"Denied or Restricted");  }});

十、日历、备忘录权限

导入头文件

检查是否有日历或者备忘录权限

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;}

获取日历、备忘录权限

EKEventStore *store = [[EKEventStore alloc]init];[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {  if (granted) {        NSLog(@"Authorized");  }else{        NSLog(@"Denied or Restricted");  }}];


0 0
原创粉丝点击