iOS相机授权 - 错误状态

时间:2015-12-28 18:48:31

标签: ios camera authorization

我使用以下代码检查并请求相机的授权。问题如下。以下方案导致授权状态错误:

  1. 用户首次拒绝授权
  2. 终止应用
  3. 重新启动应用
  4. 离开应用程序,在设置应用程序中授予相机授权
  5. 返回应用
  6. [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]将返回AVAuthorizationStatusDeclined(如上所述授权)。

    AVAuthorizationStatusAuthorized之后终止并重新启动结果。在这种情况下,用户离开设置并拒绝摄像头访问,结果将保持AVAuthorizationStatusAuthorized,直到下次重新启动。

    我想念的是什么想法?

    - (void) popCamera {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //picker.allowsEditing = YES;
        #if !(TARGET_IPHONE_SIMULATOR)
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        #endif
        self.view.translatesAutoresizingMaskIntoConstraints = YES;
        [self presentViewController:picker animated:YES completion:NULL];
    }
    
    - (void)camDenied
    {
        NSLog(@"%@", @"Denied camera access");
    
        NSString *alertText;
        NSString *alertButton;
    
        BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
        if (canOpenSettings)
        {
            alertText = LSS(@"DeniedCamera1");
    
            SDCAlertView *alert = [[SDCAlertView alloc]
                                  initWithTitle:LSS(@"DeniedCameraTitle")
                                  message:alertText
                                  delegate:self
                                  cancelButtonTitle:LSS(@"Cancel")
                                  otherButtonTitles:LSS(@"Goto"), nil];
            alert.tag = 3491832;
            [alert show];
        }
        else
        {
            alertText = LSS(@"DeniedCamera2");
    
            SDCAlertView *alert = [[SDCAlertView alloc]
                                  initWithTitle:LSS(@"DeniedCameraTitle")
                                  message:alertText
                                  delegate:self
                                  cancelButtonTitle:LSS(@"Cancel")
                                  otherButtonTitles:nil];
            alert.tag = 3491832;
            [alert show];
        }
    
    }
    
    - (IBAction) onTakePhoto:(id)sender {
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if(authStatus == AVAuthorizationStatusAuthorized)
        {
            [self popCamera];
        }
        else if(authStatus == AVAuthorizationStatusNotDetermined)
        {
            NSLog(@"%@", @"Camera access not determined. Ask for permission.");
    
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
             {
                 if(granted)
                 {
                     [self popCamera];
                 }
                 else
                 {
                     [self camDenied];
                 }
             }];
        }
        else if (authStatus == AVAuthorizationStatusRestricted)
        {
            SDCAlertView *alert = [[SDCAlertView alloc]
                                  initWithTitle:LSS(@"RestrictCameraTitle")
                                  message:LSS(@"RestrictCamera")
                                  delegate:self
                                  cancelButtonTitle:LSS(@"OK")
                                  otherButtonTitles:nil];
        }
        else
        {
            [self camDenied];
        }
    
        }
    

    原始代码的积分:Is there a way to ask user for Camera access after they have already denied it on iOS 8?

1 个答案:

答案 0 :(得分:0)

这似乎是预期的行为。如果Apple希望您在运行时对授权更改做出反应,则会有通知告诉您它已更改。

但是现在,there is no such notification(据我所见)。您只需致电+authorizationStatusForMediaType:,它就会返回明确的状态(例如拒绝或授权),或者返回AVAuthorizationStatusNotDetermined告诉您需要通过requestAccessForMediaType:completionHandler:请求授权。

不幸的是,这不是一个权威的答案;我只是在这里得出结论和猜测。您可能想在Apple的开发者论坛上询问并希望得到Apple工程师的回答。