1. 首先引入相关头文件
#import <AVFoundation/AVFoundation.h> // 麦克风、相机
#import <Photos/Photos.h> // 相机、相册
#import <CoreLocation/CoreLocation.h> // 定位
#import <CoreBluetooth/CoreBluetooth.h> // 蓝牙
2. plist文件添加key
<key>NSMicrophoneUsageDescription</key>
<string>开启麦克风权限</string>
<key>NSCameraUsageDescription</key>
<string>开启相机权限</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>开启相册权限</string>
<key>NSLocationUsageDescription</key>
<string>开启位置权限</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>使用期间开启位置权限</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>始终开启位置权限</string>
3. 麦克风权限
// 获取麦克风权限状态
- (BOOL)getMicrophonePermissionStatus
{
AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (microPhoneStatus <= 2) {
// 未授权 or 被拒绝
return NO;
}
return YES;
}
// 开启麦克风权限
- (void)openMicrophonePermissionMethod
{
AVAuthorizationStatus microPhoneStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (microPhoneStatus) {
case AVAuthorizationStatusNotDetermined:
{ // 未授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
NSLog(@"麦克风权限开启结果 --- %d", granted);
}];
}
break;
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
{
// 被拒绝 --- 跳转APP设置页
}
break;
case AVAuthorizationStatusAuthorized:
{
// 有授权 --- 跳转APP设置页
}
break;
default:
break;
}
}
4. 相机权限
// 获取相机权限状态
- (BOOL)getCameraPermissionStatus
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status <= 2) {
return NO;
}
return YES;
}
// 开启相机权限
- (void)openCameraPermissionMethod
{
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status) {
case AVAuthorizationStatusNotDetermined:
{ // 未授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
NSLog(@"相机权限开启状态 --- %d", granted);
}];
}
break;
case AVAuthorizationStatusRestricted:
case AVAuthorizationStatusDenied:
{ // 被拒绝 --- 跳转APP设置页
}
break;
case AVAuthorizationStatusAuthorized:
{ // 已授权 --- 跳转APP设置页
}
break;
default:
break;
}
}
5. 相册权限
// 获取相册权限状态
- (BOOL)getAlbumPermissionStatus
{
if (@available(iOS 14.0, *)) {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:(PHAccessLevelReadWrite)];
if (status <= 2) { // 未授权 or 被拒绝
return NO;
}
}
else {
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status <= 2) { // 未授权 or 被拒绝
return NO;
}
}
return YES;
}
// 开启相册权限
- (void)openAlbumPermissionMethod
{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status) {
case AVAuthorizationStatusNotDetermined:
{ // 未授权
if (@available(iOS 14.0, *)) {
[PHPhotoLibrary requestAuthorizationForAccessLevel:(PHAccessLevelReadWrite) handler:^(PHAuthorizationStatus status) {
NSLog(@"ios14以上 - 相册权限开启结果 --- %ld", (long)status);
}];
}
else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
NSLog(@"ios14以下 - 相册权限开启结果 --- %ld", (long)status);
}];
}
}
break;
case AVAuthorizationStatusRestricted:
case AVAuthorizationStatusDenied:
{
// 被拒绝 --- 跳转APP设置页
}
break;
case AVAuthorizationStatusAuthorized:
{
// 已授权 --- 跳转APP设置页
}
break;
default:
break;
}
}
6. 定位权限
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation ViewController
// 获取定位权限状态
- (BOOL)getLocationPermissionStatus
{
CLAuthorizationStatus locationStatus = [CLLocationManager authorizationStatus];
if ([CLLocationManager locationServicesEnabled] && locationStatus > 2) {
return YES;
}
return NO;
}
// 开启定位权限
- (void)openLocationPermissionMethod
{
CLAuthorizationStatus locationStatus = [CLLocationManager authorizationStatus];
if (locationStatus == kCLAuthorizationStatusNotDetermined) {
// 未授权
[self.locationManager requestWhenInUseAuthorization];
}
else {
// 被拒绝 or 已授权 --- 跳转APP设置页
}
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"locationManager --- %d", status);
}
- (CLLocationManager *)locationManager
{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return _locationManager;
}
@end
网友评论