Joe Blow Joe Blow - 1 year ago 164
iOS Question

iOS10, camera permission - black screen for "camera app"

Of course with iOS 10, you now have to do THIS

enter image description here

to use the phone's camera. On first launch, the user gets a question such as,

enter image description here

and all is well.

BUT we have a client app that is a "camera app": when you launch the app it simply immediately launches the camera, when the app is running the camera is running and is shown fullscreen. The code to do so is the usual way, see below.

The problem is - the first launch of the app on a phone, the user is asked the question; user says yes. But then, the camera is just black on devices we have tried. It does not crash (as it would if you forget the plist item) but it goes black and stays black.

If the user quits the app and launches it again - it's fine, everything works.

What the heck is the workflow for a "camera app"? I can't see a good solution, but there must be one for the various camera apps out there - which immediately go to fullscreen camera when you launch the app.

class CameraPlane:UIViewController
func cameraBegin()
captureSession = AVCaptureSession()
captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

var error: NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError
error = error1
input = nil

if ( error != nil )
print("probably on simulator? no camera?")

if ( captureSession!.canAddInput(input) == false )
print("capture session problem?")


stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

if ( captureSession!.canAddOutput(stillImageOutput) == false )
print("capture session with stillImageOutput problem?")

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
// or, AVLayerVideoGravityResizeAspect


previewLayer!.frame = view.bounds

Answer Source

Note: it's likely OP's code was actually working correctly in terms of the new ISO10 permission string, and OP had another problem causing the black screen.

From the code you've posted, I can't tell why you experience this kind of behavior. I can only give you the code that is working for me.

This code also runs on iOS 9. Note that I am loading the camera in viewDidAppear to make sure that all constraints are set.

import AVFoundation

class ViewController : UIViewController {

    //the view where the camera feed is shown
    @IBOutlet weak var cameraView: UIView!

    var captureSession: AVCaptureSession = {
        let session = AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetPhoto
        return session
    var sessionOutput = AVCaptureStillImageOutput()
    var previewLayer = AVCaptureVideoPreviewLayer()

    override func viewDidAppear(_ animated: Bool) {

        let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as? [AVCaptureDevice]
        guard let backCamera = (devices?.first {    $0.position == .back    }) else {
            print("The back camera is currently not available")

        do {
            let input = try AVCaptureDeviceInput(device: backCamera)
            if captureSession.canAddInput(input){
                sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

                if captureSession.canAddOutput(sessionOutput) {

                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                    previewLayer.connection.videoOrientation = .portrait

                    previewLayer.position = CGPoint(x: cameraView.frame.width / 2, y: cameraView.frame.height / 2)
                    previewLayer.bounds = cameraView.frame
        } catch {
            print("Could not create a AVCaptureSession")

If you want the camera to show fullscreen you can simply use view instead of cameraView. In my camera implementation the camera feed does not cover the entire view, there's still some navigation stuff.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download