bear-zyy

权限检测

ios开发中检测相机、麦克风、通知权限

这个是获取到权限后的枚举:

1
2
3
4
AVAuthorizationStatusNotDetermined = 0,//未询问过用户是否授权
AVAuthorizationStatusRestricted, //未授权,例如家长控制
AVAuthorizationStatusDenied, //未授权,用户曾选择过拒绝授权
AVAuthorizationStatusAuthorized //已经授权

相机权限检测

1
2
3
4
5
6
7
8
9
10
AVAuthorizationStatus atatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (atatus == AVAuthorizationStatusDenied || atatus == AVAuthorizationStatusRestricted) {
UIAlertController *alert =[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"请在手机的”设置-隐私-相机“中允许访问您的相机。"] message:@"" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}]];
[self presentViewController:alert animated:NO completion:nil];
return NO;
}

麦克风权限检测

1
2
3
4
5
6
7
8
9
10
AVAuthorizationStatus AVatatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风
if (AVatatus == AVAuthorizationStatusDenied || AVatatus == AVAuthorizationStatusRestricted) {
UIAlertController *alert =[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"请在手机的”设置-隐私-麦克风“中允许访问您的麦克风。"] message:@"" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}]];
[self presentViewController:alert animated:NO completion:nil];
return NO;
}

通知权限检测

注本文只提供ios8以后

1
2
3
4
5
6
7
UIUserNotificationSettings * setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (setting.types == UIUserNotificationTypeNone) {//这个是关闭了推送
UIAlertController *alert =[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"您关闭了推送通知"] message:@"" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}]];
[self presentViewController:alert animated:NO completion:nil];
}
⬅️ Go back