Ignorance is a kind of beauty

Writing in the front

  • The problem of system permissions cannot be avoided in APP development. How to show system permissions to users in a more friendly way seems to be a matter worth thinking about in the development process.
  • So how to improve the approval rate of APP obtaining iOS system permissions? There are several ways :1. Request permission to the user when the user opens the APP; 2. 2. Inform the user of the permission to obtain benefits, and then request permission from the user. 3. Request permission only when it is absolutely necessary. For example, when a user accesses the photo library, request permission to access the system album. 4. Before the system permission dialog box is displayed, a customized dialog box is displayed for the user. If the user disallows the dialog box, no operation is performed by default.

  • The above situation is often encountered in the development process, and the choice of different methods can affect the final user interaction experience. This insight comes from the problem I encountered last week: adapting to iOS10, how to obtain application networking permission to manage the display management of system dialog box. When I solved this problem, I felt it was necessary to make a summary of the commonly used iOS system permissions for later use.

Classification of permissions

  • Connected to the Internet access
  • Photo album permissions
  • Camera and microphone permissions
  • Location permissions
  • Push permissions
  • Address book rights
  • Calendar and memo permissions

Connected to the Internet access

  • Import header @import CoreTelephony;
  • After the application is started, check whether the application has Internet access permission

    CTCellularData *cellularData = [[CTCellularData alloc]init]; CellularData. CellularDataRestrictionDidUpdateNotifier = ^ (CTCellularDataRestrictedState state) {/ / access network state switch (state) { case kCTCellularDataRestricted: NSLog(@"Restricrted"); break; case kCTCellularDataNotRestricted: NSLog(@"Not Restricted"); break; case kCTCellularDataRestrictedStateUnknown: NSLog(@"Unknown"); break; default: break; }; };Copy the code
  • Example Query whether the application has the networking function

    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;
    }Copy the code

Album permissions – before iOS 9.0

  • Import header @import AssetsLibrary;
  • Check whether you have album permission

     ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
    switch (status) {
      case ALAuthorizationStatusAuthorized:
          NSLog(@"Authorized");
          break;
      case ALAuthorizationStatusDenied:
          NSLog(@"Denied");
          break;
      case ALAuthorizationStatusNotDetermined:
          NSLog(@"not Determined");
          break;
      case ALAuthorizationStatusRestricted:
          NSLog(@"Restricted");
          break;
    
      default:
          break;
    }Copy the code

Album permissions — after iOS 8.0

  • Import header @import Photos;
  • Check whether you have album permission

    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;
    }Copy the code
  • ! Uploading 144446-b8ACA7ba38C5f8c0_695906.png

     [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
      if (status == PHAuthorizationStatusAuthorized) {
          NSLog(@"Authorized");
      }else{
          NSLog(@"Denied or Restricted");
      }
      }];Copy the code

Camera and microphone permissions

  • Import header @import AVFoundation;
  • Check whether you have camera or microphone permission

    AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; / / camera permissions AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType: AVMediaTypeAudio]; / / the microphone access 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; }Copy the code
  • Obtain the camera or microphone permission

    [AVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo completionHandler: ^ (BOOL granted) {/ / camera permission if (granted) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }]; [AVCaptureDevice requestAccessForMediaType: AVMediaTypeAudio completionHandler: ^ (BOOL granted) {/ / microphone permissions if (granted) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }];Copy the code

Location permissions

  • Import header @import CoreLocation;
  • Because the location method has changed since iOS8.0, you need to configure it in info.plist.



The configuration file

  • Check whether you have the location permission

    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; }Copy the code
  • Obtaining location Rights

    CLLocationManager *manager = [[CLLocationManager alloc] init]; [manager requestAlwaysAuthorization]; / / has been positioning information [manager requestWhenInUseAuthorization]; // Get location information when usingCopy the code
  • Check to see if permissions have changed in the agent method

    - (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; }}Copy the code

Push permissions

  • Check whether you have communication permission

      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;
    }Copy the code
  • Obtain push permission

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

Address book rights

  • Import header @import AddressBook;
  • Check whether you have the address book permission

     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;
    }Copy the code
  • Obtaining Address Book Rights

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

Calendar and memo permissions

  • Import header file
  • Check whether you have calendar or memo permissions

    Typedef NS_ENUM(NSUInteger, EKEntityType) {EKEntityTypeEvent,// calendar EKEntityTypeReminder // note};Copy the code
  • 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;
    }Copy the code
  • Obtain the calendar or memo permission

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

The last point

  • After iOS 8.0, these Settings are integrated and the corresponding permissions can be enabled or disabled. All permissions can be opened using the following methods:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];Copy the code

The above permissions are often used permissions, of course not very comprehensive, if you need other permissions, you can comment below, I will add in time. Hope this article can bring some convenience to everyone’s development. Welcome to follow the wechat public ID:iOSDevSkills, or search “Siege lion dynamic”, more exciting technical articles, look forward to your joining! Discuss and grow together!