3

I have a NavigationController that is connected with UIViewController which I called ScanViewController. In that controller, I want to start the camera and I have just this simple code snippet in viewDidLoad and there is no code there beside this one:

    let vc = UIImagePickerController()
    vc.sourceType = .camera
    vc.allowsEditing = true
    present(vc, animated: true)

But immediately after the camera is started it shuts down and closes the app. The output is below. I have an orientation warning but that shouldn't shut down the camera or? I have camera permission also I have verified that by checking in the phone Settings and my app has Camera access enabled.

Permission in the code:

    switch AVCaptureDevice.authorizationStatus(for: .video)
    case .authorized:
        print("Scan View Controller || Permission to use camera granted")
        self.startCamera()

Permission in info.plist:

<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) needs access to your camera.</string>

And the logs:

2023-03-17 11:28:12.627212+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackWideDual). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:12.627307+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackWideDual). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:12.627614+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackAuto). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:12.627641+0100 Cykelrum.se[41417:2892989] [Camera] Attempted to change to mode Portrait with an unsupported device (BackWideDual). Auto device for both positions unsupported, returning Auto device for same position anyway (BackAuto).
2023-03-17 11:28:13.608818+0100 Cykelrum.se[41417:2893505] XPC connection interrupted
2023-03-17 11:28:13.611892+0100 Cykelrum.se[41417:2893494] [Common] [SBSSystemServiceClient:0x282a8d080] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:13.612078+0100 Cykelrum.se[41417:2893494] [Common] [FBSOrientationObserverClient:0x282abbea0] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:14.352022+0100 Cykelrum.se[41417:2893505] [Common] [FBSOrientationObserverClient:0x282abbea0] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:14.352115+0100 Cykelrum.se[41417:2893505] [Common] [SBSSystemServiceClient:0x282a8d080] Service suspended: the connection with the service host has been interrupted.
2023-03-17 11:28:22.302388+0100 Cykelrum.se[41417:2893494] 10.6.0 - [FirebaseAnalytics][I-ACS800014] Cannot get flag for unregistered flag. SDK name, flag name: app_measurement, session_stitching_token_feature_enabled
2023-03-17 11:28:23.630021+0100 Cykelrum.se[41417:2893414] [connection] nw_connection_copy_connected_local_endpoint_block_invoke [C27] Client called nw_connection_copy_connected_local_endpoint on unconnected nw_connection
2023-03-17 11:28:23.630078+0100 Cykelrum.se[41417:2893414] [connection] nw_connection_copy_connected_remote_endpoint_block_invoke [C27] Client called nw_connection_copy_connected_remote_endpoint on unconnected nw_connection
2023-03-17 11:28:23.630117+0100 Cykelrum.se[41417:2893414] [connection] nw_connection_copy_protocol_metadata_internal_block_invoke [C27] Client called nw_connection_copy_protocol_metadata_internal on unconnected nw_connection
2023-03-17 11:28:24.463510+0100 Cykelrum.se[41417:2893414] [connection] nw_resolver_start_query_timer_block_invoke [C25.1.1] Query fired: did not receive all answers in time for app-measurement.com:443
Yupi
  • 4,402
  • 3
  • 18
  • 37

1 Answers1

0

I got this kind of error once, when my device has faulty camera. You may need to modify your code to check the available camera modes and only use supported modes. Here is an example of how you could modify your code to do this:

if UIImagePickerController.isSourceTypeAvailable(.camera) {
    imagePickerController.sourceType = .camera

    // Check available camera modes
    if let availableCameraModes = UIImagePickerController.availableCaptureModes(for: .rear) {
        // Only use supported camera modes
        if availableCameraModes.contains(.video) {
            imagePickerController.cameraCaptureMode = .video
        } else {
            // Handle unsupported camera mode error
            print("Error: Photo capture mode not available")
            return
        }
    }

    // Present image picker controller
    present(imagePickerController, animated: true, completion: nil)
} else {
    // Handle unavailable camera error
    print("Error: Camera not available")
}