I have an app where I need to request gallery access and camera access at the same time on tap of a button.i.e. one after another.I am new to swift. Is there a proper way to do that?
This is my code I am currently using:
func checkPermissions() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
// Request read-write access to the user's photo library.
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
switch status {
case .notDetermined:
// The user hasn't determined this app's access.
print("Not Determined")
self.showAlertToOpenSettings(type: 1)
case .restricted:
// The system restricted this app's access.
print("Resticted")
self.showAlertToOpenSettings(type: 1)
case .denied:
// The user explicitly denied this app's access.
print("Denied")
self.showAlertToOpenSettings(type: 1)
case .authorized:
// The user authorized this app to access Photos data.
print("authorised")
DispatchQueue.main.async {
self.openPhotoPicker()
}
case .limited:
// The user authorized this app for limited Photos access.
print("Limited")
DispatchQueue.main.async {
self.showLimitedLibraryAlert()
}
// self.openLimitedLibrary()
@unknown default:
fatalError()
}
}
}
if UIImagePickerController.isSourceTypeAvailable(.camera) {
//Camera
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
//access granted
} else {
self.showAlertToOpenSettings(type: 2)
}
}
}
}
The issue with this is that if user selects options from photoLibrary permission alert, the camera alert appears immediately after selecting any option. For example if I select limited option ("Select Photos") then Camera alert appears immediately after over the select photos window.
Please let me know if I am missing anything from my question. I hope it is more clear now.
2
Answers
first add these lines to info plist
and also this
after that add this class in your project
how to use it inside button or viewdidload etc
You can add camera and photo gallery permission both one after another. You just have to add this key in info.plist –
<key>NSCameraUsageDescription</key> <string>Mention reason to access camera</string>
and once it is added you have to add the following code in Appdelegate or in view controller you need to ask permission –