1. Import the relevant header file first
# import < AVFoundation/AVFoundation. H > / / microphone, camera # import < Photos/Photos. H > / / camera, photo album # import < > CoreLocation/CoreLocation. H / / position # import < CoreBluetooth/CoreBluetooth. H > / / bluetoothCopy the code
2. Add a key to the plist file
< key > NSMicrophoneUsageDescription < / key > < string > open microphone permissions < / string > < key > NSCameraUsageDescription < / key > < string > open camera permission < / string > < key > NSPhotoLibraryUsageDescription < / key > < string > open photo album permissions < / string > < key > NSLocationUsageDescription < / key > < string > open position permissions < / string > < key > NSLocationWhenInUseUsageDescription < / key > < string > using open position during permissions < / string > < key > NSLocationAlwaysUsageDescription < / key > < string > always open position permissions < / string >Copy the code
3. Microphone permission
/ / get the microphone permissions state - (BOOL) getMicrophonePermissionStatus {AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]; If (microPhoneStatus <= 2) {// Unauthorized or rejected return NO; } return YES; } / / open the microphone permissions - (void) openMicrophonePermissionMethod {AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]; switch (microPhoneStatus) { case AVAuthorizationStatusNotDetermined: {/ / unauthorized [AVCaptureDevice requestAccessForMediaType: AVMediaTypeAudio completionHandler: ^ (BOOL granted) { NSLog(@" microphone permission enabled result -- %d", granted);}]; } break; Case AVAuthorizationStatusDenied: case AVAuthorizationStatusRestricted: {/ / rejected} - jump APP Settings page break; Case AVAuthorizationStatusAuthorized: {/ / have authorized} - jump APP Settings page break; default: break; }}Copy the code
4. Camera permission
/ / get the camera permissions state - (BOOL) getCameraPermissionStatus {AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if (status <= 2) { return NO; } return YES; } / / open camera permissions - (void) openCameraPermissionMethod {AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; switch (status) { case AVAuthorizationStatusNotDetermined: {/ / unauthorized [AVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo completionHandler: ^ (BOOL granted) { NSLog(@" camera permission enabled -- %d", granted);}]; } break; Case AVAuthorizationStatusRestricted: case AVAuthorizationStatusDenied: {/ / rejected} - jump APP Settings page break; Case AVAuthorizationStatusAuthorized: {/ / authorized} - jump APP Settings page break; default: break; }}Copy the code
5. Album permission
- (BOOL)getAlbumPermissionStatus {if (@available(iOS 14.0, *)) { PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:(PHAccessLevelReadWrite)]; If (status <= 2) {return NO; } } else { PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; If (status <= 2) {return NO; } } return YES; } / / open photo album permissions - (void) openAlbumPermissionMethod {PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; switch (status) { case AVAuthorizationStatusNotDetermined: {// unauthorized if (@available(iOS 14.0, *)) { [PHPhotoLibrary requestAuthorizationForAccessLevel:(PHAccessLevelReadWrite) handler:^(PHAuthorizationStatus Status) {NSLog(@"ios14 > -- %ld", (long)status);}]; } else {PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {NSLog(@"ios14 below - album permission enabled result -- %ld", (long)status); }]; } } break; Case AVAuthorizationStatusRestricted: case AVAuthorizationStatusDenied: {/ / rejected} - jump APP Settings page break; Case AVAuthorizationStatusAuthorized: {/ / authorized} - jump APP Settings page break; default: break; }}Copy the code
6. Locate the permission
@interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @ end @ implementation ViewController / / for locating authority state - (BOOL) getLocationPermissionStatus {CLAuthorizationStatus locationStatus = [CLLocationManager authorizationStatus]; if ([CLLocationManager locationServicesEnabled] && locationStatus > 2) { return YES; } return NO; } / / open location permissions - (void) openLocationPermissionMethod {CLAuthorizationStatus locationStatus = [CLLocationManager authorizationStatus]; If (locationStatus = = kCLAuthorizationStatusNotDetermined) {/ / unauthorized [self. The locationManager requestWhenInUseAuthorization]; } else {// rejected or authorized -- jump APP Settings page}} - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { NSLog(@"locationManager --- %d", status); } - (CLLocationManager *)locationManager { if (! _locationManager) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; } return _locationManager; } @endCopy the code