0

This iOS app uses the camera for an image to be placed in the detail view of a master detail app. When the app is installed and the take photo action is requested for the first time, the app crashes. As seen in the code, access is requested and the user is presented with an alert requesting access. Once granted the completion handler reports success and the app crashes. If I restart the app (without reinstalling it), the camera performs as expected and continues to do so. Removing the app from the iPhone and reinstalling always produces exactly the same result.

The console output: AVAuthorizationStatus is NotDetermined.
The user granted permission.
libc++abi.dylib: terminating with uncaught exception of type NSException

Any help would be appreciated. iOS 10, Xcode 8, Swift 3

Edit: It crashes in the AVCaptureRequest. If I comment out the AVCaptureRequest the app does not crash (of course with no photo).

@IBAction func takeDrugPhoto(_ sender: UIButton) {


    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {

        let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)

        switch authStatus {

        case AVAuthorizationStatus.authorized:
            print("AVAuthorizationStatus is Authorized")

.....bunch of code here......

        case AVAuthorizationStatus.denied:
            print("AVAuthorizationStatus is Denied")

.....some code here .....

        case AVAuthorizationStatus.notDetermined:
            print("AVAuthorizationStatus is NotDetermined")


            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (success) in

                if success {
                    print("The user granted permission")

                } else {
                    print("put up an alert telling the user the camera is not available")

                    DispatchQueue.main.async(execute: { () -> Void in
                        let ac = UIAlertController(title: "Camera Error", message: "For some reason, the camera in this device is not accepting your authorization. Check with your device supplier.", preferredStyle: .alert)
                        ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
                        self.present(ac, animated: true, completion: nil)
                    })//back on main queue block
                }//if success

            })//requestAccessForMediaType block


        }//switch

    } else {//if device has a camera

        let ac = UIAlertController(title: "Source not available.", message: "The camera is not available.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(ac, animated: true, completion: nil)

    }//if camera is no else

}//takeDrugPhoto

From the trace:

JohnSF
  • 3,736
  • 3
  • 36
  • 72

1 Answers1

0

When you seek user permission through an AVCaptureDevice.requestAccess request, iOS presents an alert dialog that incorporates the message you create in the info.plist. When the user grants permission, it appears as thought iOS then treats the app as if it were returning from the background (even though it does not disappear). That was my issue. I had structured a login sequence to activate whenever the app returns from the background as part of the security. I had disabled that interface for testing - hence the failures. I hope others will find this useful.

JohnSF
  • 3,736
  • 3
  • 36
  • 72