ios – Digicam doesn’t load after giving permission on first press however works on observe up presses swiftui

ios – Digicam doesn’t load after giving permission on first press however works on observe up presses swiftui


I’m new to swiftUI and I’m constructing an expense tracker app to be taught. I would like the consumer to have the ability to take a picture of a receipt or choose a picture utilizing the photopicker.

At present each implementations work however there’s a slight challenge.
When the consumer presses take photograph the permissions alert shows asking the consumer if they’d give the applying permission to make use of the digicam. In the event that they choose sure the photopicker shows to pick out a photograph quite than the precise digicam performance.If the consumer then clicks cancel and presses take photograph the digicam shows appropriately because it ought to.

How do I eliminate this bug and permit the digicam to show as quickly as permission is granted? I attempted including a delay with dispatch queue however this didn’t work. Beneath is my present code.

                                    .default(Textual content("Take Picture")) {
                                        let cameraAuthorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
                                        swap cameraAuthorizationStatus {
                                        case .notDetermined:
                                            // Request permission for the digicam
                                            AVCaptureDevice.requestAccess(for: .video) { granted in
                                                DispatchQueue.foremost.async {
                                                    if granted {
                                                        if UIImagePickerController.isSourceTypeAvailable(.digicam) {
                                                            sourceType = .digicam
                                                            showImagePicker = true
                                                        } else {
                                                            showPermissionAlert = true
                                                            permissionAlertMessage = "Digicam isn't accessible on this gadget."
                                                            showSettingsButton = false // No want for a Settings button
                                                        }
                                                    } else {
                                                        showPermissionAlert = true
                                                        permissionAlertMessage = "Digicam entry has been denied. Please allow it in Settings."
                                                        showSettingsButton = true // Embrace a Settings button
                                                    }
                                                }
                                            }
                                        case .licensed:
                                            // Permission already granted
                                            if UIImagePickerController.isSourceTypeAvailable(.digicam) {
                                                sourceType = .digicam
                                                showImagePicker = true
                                            } else {
                                                showPermissionAlert = true
                                                permissionAlertMessage = "Digicam isn't accessible on this gadget."
                                                showSettingsButton = false // No want for a Settings button
                                            }
                                        case .denied, .restricted:
                                            // Permission denied or restricted
                                            showPermissionAlert = true
                                            permissionAlertMessage = "Digicam entry has been denied. Please allow it in Settings."
                                            showSettingsButton = true // Embrace a Settings button
                                        @unknown default:
                                            showPermissionAlert = true
                                            permissionAlertMessage = "An unknown error occurred. Please attempt once more."
                                            showSettingsButton = false // Default to no Settings button
                                        }
                                    }

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.