0

I'm attempting to control flow for an application that needs access to the device camera. The idea is to check the setting for camera access and if camera access is not allowed, give the user the opportunity to go directly to the settings for the app. Then once the switch for camera is changed (or not) return the user to the app.

The following code seems to do that when the app is run from the device. However, if the device is tethered and the app starts from Xcode, the instant the switch is touched, the App crashes. There is no information written to the console - just the dreaded highlight of the AppDelegate first line. Obviously, I don't trust that it is actually "working" on the device.

Any help would be appreciated.

Xcode 7.2.1 IOS 9.2.1

var userOkForCamera : Bool = false


@IBAction func takeInvItemPhoto(sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {

        if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized {
            // Already Authorized
            // This seems to work ok when the use auth switch is On

            userOkForCamera = true

            let picker = UIImagePickerController()
            picker.delegate = self
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.mediaTypes = [kUTTypeImage as String]
            picker.allowsEditing = false
            presentViewController(picker, animated: true, completion: nil)

        } else {
            userOkForCamera = false
        }//if auth status else

        if userOkForCamera == false {
            showCameraAcessDeniedAlert()
            return
        }// if user ok false

    } else {//if 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))
                presentViewController(ac, animated: true, completion: nil)

    }//if camera else

}//takeInvItemPhoto

    func showCameraAcessDeniedAlert() {
    let alertController = UIAlertController(title: "Uh-ooh!",
        message: "It looks like camera permission is not authorized. Please enable it in Settings to continue.",
        preferredStyle: .Alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .Default) { (alertAction) in

        if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
            UIApplication.sharedApplication().openURL(appSettings)
        }//if let
    }//settings action block

    alertController.addAction(settingsAction)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
    alertController.addAction(cancelAction)

    presentViewController(alertController, animated: true, completion: nil)

}//showCameraAcessDeniedAlert

enter image description here

JohnSF
  • 3,736
  • 3
  • 36
  • 72
  • Well. It may be a duplicate for you, but not for me. As stated, with the app running on the device, the code works. Granted, I've only tested it a couple of dozen times, but on the device the app has not crashed a single time. In fact, Apple automatically adds a link in the top left corner to return the user to the app. That link does return the user to the app which continues to run. I have ONLY experienced the problem when tethered and starting from Xcode. Maybe the end result is the same, but if Apple adds the link to return to the app, they must have an expectation that it will work. – JohnSF Mar 15 '16 at 05:37

1 Answers1

0

An iOS app is killed when the user adjusts a system setting relating to it. There have been a number of questions around this, e.g. https://stackoverflow.com/a/12938640/1153630

Community
  • 1
  • 1
Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59
  • Hi. Thanks for your interest. Please see my note above. The link you reference is seriously old and I do not believe IOS behaves that way now. – JohnSF Mar 15 '16 at 06:06