一、没有打开权限,直接打开导致闪退
判断权限
`
AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
**switch** (videoAuthStatus) {
// 已授权
**case** AVAuthorizationStatusAuthorized:
{
**if**(permissionGranted){
permissionGranted(**YES**);
}
}
**break**;
// 未询问用户是否授权
**case** AVAuthorizationStatusNotDetermined:
{
// 提示用户授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(**BOOL** granted) {
**if**(permissionGranted){
permissionGranted(granted);
}
}];
}
**break**;
// 用户拒绝授权或权限受限
**case** AVAuthorizationStatusRestricted:
**case** AVAuthorizationStatusDenied:
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请在”设置-隐私-相机”选项中,允许访问你的相机" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:**nil**];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * **_Nonnull** action) {
UIApplication *app = [UIApplication sharedApplication];
NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
**if** ([app canOpenURL:settingURL]) {
[app openURL:settingURL];
}
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[controller presentViewController:alertController animated:**YES** completion:**nil**];
**if**(permissionGranted){
permissionGranted(**NO**);
}
}
**break**;
**default**:
**break**;
}
`
二、手机坏了拍不了照
`
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"该设备不支持拍照" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:**nil**];
[alertController addAction:cancelAction];
[controller presentViewController:alertController animated:**YES** completion:**nil**];
**if**(permissionGranted){
permissionGranted(**NO**);
}
**return**;
}
`
三、不支持扫码或人脸识别
为了统一,我把可支持的类型都丢进去了(最好按需加入)
`
NSArray *availableMetadataObjectTypes = metadataOutput.availableMetadataObjectTypes.mutableCopy;
**if**(![availableMetadataObjectTypes isKindOfClass:NSArray.class] || availableMetadataObjectTypes.count<1 || ![availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]){
[SDRCommonToolManager_SAAS showInfoWithStatus:@"该手机不支持二维码扫描"];
**return**;
}
`
四、你要扫码区返回人脸,你要人脸却放回扫码
判断放回类型
`
- (void)captureOutput:(AVCaptureOutput *)output didOutputMetadataObjects:(NSArray< __kindof AVMetadataObject *> *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
// 获取扫一扫结果
**if** (metadataObjects && metadataObjects.count > 0) {
[**self** pauseScanning];
NSString *stringValue = **nil**;
//处理扫描结果
**for** (AVMetadataMachineReadableCodeObject *metadataObject **in** metadataObjects) {
**if**([metadataObject isKindOfClass:AVMetadataMachineReadableCodeObject.class]){//二维码
stringValue = metadataObject.stringValue;
}**else** **if** ([metadataObject isKindOfClass:AVMetadataFaceObject.class]){//人脸
}
}
**if**(stringValue){
**if** (**self**.manager.scanDelegate) {
[**self**.manager.scanDelegate ly_QRCodeScanResultText:stringValue];
}
[**self** popBack];
}
}
}
`
网友评论