App開發許可權

雲水天川-個人軟體開發者發表於2016-07-28

一、請求許可權的方式

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


相關文章