IOS->开发技巧iOS日常开发

iOS10判断摄像机是否可用

2016-11-17  本文已影响1317人  xiao小马哥
97G58PICw6n_1024.jpg

自言自语:(最近想买一直比较忙,都很少有时间写点东西和大家分享,要坚持写点东西啊!)

iOS10以后增加了权限的管理,更注重用户的隐私

我们在开发的时候避免不了会调用用户的相机或者相册,在iOS10以后就需要在info.plist文件里添加这两个key Privacy - Camera Usage DescriptionPrivacy - Photo Library Usage Description 分别对应:相机权限相册权限,具体的key查看这篇文章ios10要添加的key
虽然添加了这个key用户在使用我们app的时候,当初次使用到相机的时候,如果用户不允许,那么下次在使用的时候就不会在提示,如果你不做判断还继续使用你的相机,很有可能造成崩溃的bug(别觉得我说的很严重,我做的二维码扫描就遇到这样的问题了)

废话不多说,上代码

在需要掉摄像头的地方

  /// 先判断摄像头硬件是否好用
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    /// 用户是否允许摄像头使用
    NSString * mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus  authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  /// 不允许弹出提示框
    if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied) {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"摄像头访问受限" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:nil];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertC addAction:action];
    }else{
  ////这里是摄像头可以使用的处理逻辑
}   
} else {
  /// 硬件问题提示
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"手机摄像头设备损坏" message:@"" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    [alertView show];
}
上一篇 下一篇

猜你喜欢

热点阅读