Powered by md-Blog  文 - 篇  访客 -

iOS开发之隐私权限


  分类:iOS开发  / 
更新:2020-11-10 15:02:04  /  创建:2020-11-12 17:35:16
不要删除

iOS开发之隐私权限

1、权限判断 和 申请

1.1 网络权限

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

1.2 相册权限 ( iOS 8.0以上)

@import Photos; 导入头文件

//检查是否有相册权限
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");
  }
  }];

1.3 相机和麦克风权限

@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");
  }
}];

1.4 定位权限

@import CoreLocation;   //导入头文件

//检查定位功能是否可用
 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;
  }

}

1.5 推送权限

//检查是否有推送权限
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];

1.6通讯录权限 (iOS9.0及以后)

@import Contacts;  //导入头文件

//检查是否有通讯录权限
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
  switch (status) {
        case CNAuthorizationStatusAuthorized:
        {
            NSLog(@"Authorized:");
        }
            break;
        case CNAuthorizationStatusDenied:{
            NSLog(@"Denied");
        }
            break;
        case CNAuthorizationStatusRestricted:{
            NSLog(@"Restricted");
        }
            break;
        case CNAuthorizationStatusNotDetermined:{
             NSLog(@"NotDetermined");
        }
            break;
}

//获取通讯录权限
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted) {
    
       NSLog(@"Authorized");
    
    }else{
    
       NSLog(@"Denied or Restricted");
    }
 }];

1.7 日历、备忘录权限

@import EventKit; //导入头文件

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");
  }
}];

2、info.plist文件设置

<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>
 
<key>NSBluetoothPeripheralUsageDescription</key>
<string>App需要您的同意,才能访问蓝牙</string>
 
<key>NSCalendarsUsageDescription</key>
<string>App需要您的同意,才能访问日历</string>
 
<key>NSCameraUsageDescription</key>
<string>App需要您的同意,才能访问相机</string>
 
<key>NSHealthShareUsageDescription</key>
<string>App需要您的同意,才能访问健康分享</string>
 
<key>NSHealthUpdateUsageDescription</key>
<string>App需要您的同意,才能访问健康更新 </string>
 
<key>NSHomeKitUsageDescription</key>
<string>App需要您的同意,才能访问HomeKit</string>
 
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>"App需要您的同意,才能始终访问位置</string>
 
<key>NSMicrophoneUsageDescription</key>
<string>App需要您的同意,才能访问麦克风</string>
 
<key>NSMotionUsageDescription</key>
<string>App需要您的同意,才能访问运动与健身</string>
 
<key>NSPhotoLibraryAddUsageDescription</key>
<string>App需要您的同意,才能添加照片视频到您的手机</string>
 
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>

不要删除