Common permissions in iOS development

Keywords: iOS Attribute Database network

Common permissions in iOS development

Before obtaining permissions, users can be asked in several ways:

  • Request permissions from users when they open APP
  • After informing the user of the authorization rights, the user can obtain the benefits, and then request the rights from the user.
  • Request permissions from users only when absolutely necessary, for example, when users access the photo library, request permissions to access the system album
  • Before displaying the dialog box of system privileges, display the customized dialog box to the user first. If the user chooses not to allow, there is no operation by default. If the user chooses to allow, then show the system dialog box.

Commonly used permissions include: networking permissions, album permissions, camera and microphone permissions, location permissions, push permissions, address book permissions, calendar and memo permissions;

Note that all of these need to be configured in info.plist:
Adding an attribute will automatically prompt you when you enter Privacy:

  • Microphone permissions: Does Privacy - Microphone Usage Description allow this App to use your microphone?
  • Camera privileges: Does Privacy - Camera Usage Description allow this App to use your camera?
  • Album permissions: Does Privacy - Photo Library Usage Description allow this App to access your media database?
  • Address Book Permissions: Does Privacy - Contacts Usage Description allow this App to access your address book?
  • Bluetooth privileges: Is Privacy - Bluetooth Peripheral Usage Description permissible for this App to use Bluetooth?
  • Voice-to-text permission: Does Privacy-Speech Recognition Usage-Description allow this App to use speech recognition?
  • Calendar permissions: Does Privacy - Calendars Usage Description allow this App to use the calendar?
  • Location permission: Privacy - Location When In Use Usage Description We need to get relevant data around you through your geographic location information
  • Location permission: Privacy - Location Always Usage Description We need to get the relevant data around you through your geographic location information. (The positioning needs to be written so as to prevent rejection of shelves.)

Internet access

Header file: @import CoreTelephony

Detection authority:

CTCellularData *cellularData = [[CTCellularData alloc]init];  
cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){  
  //Getting the status of the network  
  switch (state) {  
      case kCTCellularDataRestricted:  
          NSLog(@"Restricrted");  
          break;  
      case kCTCellularDataNotRestricted:  
          NSLog(@"Not Restricted");  
          break;  
      case kCTCellularDataRestrictedStateUnknown:  
          NSLog(@"Unknown");  
          break;  
      default:  
          break;  
  };  
}; 

Query whether there is 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;  
}  

Album permissions (after iOS8):

Header file: @import Photos

Detection authority:

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

Access permissions:

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

Camera and microphone rights:

Header file: @import AVFoundation

Detection authority:

AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//Camera privileges  
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//Microphone permission  

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

Access permissions:

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//Camera privileges if (granted) { NSLog(@"Authorized"); }else{ NSLog(@"Denied or Restricted"); } }]; [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//Microphone permission  
  if (granted) {  
      NSLog(@"Authorized");  
  }else{  
      NSLog(@"Denied or Restricted");  
  }  
}];  

Location authority:

Header file: @import CoreLocation

Detection authority:

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

Access permissions:

CLLocationManager *manager = [[CLLocationManager alloc] init];  
[manager requestAlwaysAuthorization];//Always get location information  
[manager requestWhenInUseAuthorization];//Get location information when using  

View permission changes in proxy methods:

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

Push permission:

Detection authority:

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

Access permissions:

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

Address Book permissions (before ios9):

Header file: @import AddressBook

Detection authority:

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

Access permissions:

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

Address Book permissions (after ios9):

Header file: @import Contacts

Detection authority:

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;  

       }

Access permissions:

CNContactStore *contactStore = [[CNContactStore alloc] init];  
    [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {  
        if (granted) {  

           NSLog(@"Authorized");  

        }else{  

           NSLog(@"Denied or Restricted");  
        }  
    }];  

Calendar, Memorandum Authority:

typedef NS_ENUM(NSUInteger, EKEntityType) {  
 EKEntityTypeEvent,//calendar  
 EKEntityTypeReminder //Memo  
};

Detection authority:

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

Access permissions:

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

After iOS 8.0, these settings are integrated and the corresponding permissions can be turned on or off. All permissions can be opened by:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
UIApplicationOpenSettingsURLString]];

Reference article: http://www.jianshu.com/p/27e57922232b

Posted by depraved on Thu, 27 Jun 2019 15:09:22 -0700