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.
CodePudding user response:
first add these lines to info plist
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access camera!.</string>
and also this
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access photos.</string>
after that add this class in your project
import Photos
import UIKit
public extension PHPhotoLibrary {
static func execute(controller: UIViewController,
onAccessHasBeenGranted: @escaping () -> Void,
onAccessHasBeenDenied: (() -> Void)? = nil) {
let onDeniedOrRestricted = onAccessHasBeenDenied ?? {
let alert = UIAlertController(
title: "We were unable to load your album groups. Sorry!",
message: "You can enable access in Privacy Settings",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: { _ in
if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(settingsURL)
}
}))
controller.present(alert, animated: true)
}
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .notDetermined:
onNotDetermined(onDeniedOrRestricted, onAccessHasBeenGranted)
case .denied, .restricted:
onDeniedOrRestricted()
case .authorized:
onAccessHasBeenGranted()
case .limited:
onAccessHasBeenGranted()
@unknown default:
fatalError("PHPhotoLibrary::execute - \"Unknown case\"")
}
}
}
private func onNotDetermined(_ onDeniedOrRestricted: @escaping (()->Void), _ onAuthorized: @escaping (()->Void)) {
PHPhotoLibrary.requestAuthorization({ status in
switch status {
case .notDetermined:
onNotDetermined(onDeniedOrRestricted, onAuthorized)
case .denied, .restricted:
onDeniedOrRestricted()
case .authorized:
onAuthorized()
case .limited:
onAuthorized()
@unknown default:
fatalError("PHPhotoLibrary::execute - \"Unknown case\"")
}
})
}
how to use it inside button or viewdidload etc
PHPhotoLibrary.execute(controller: self, onAccessHasBeenGranted: {
// access granted...
})
CodePudding user response:
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>
<key>NSPhotoLibraryUsageDescription</key>
<string>Mention reason to access photo Library</string>
and once it is added you have to add the following code in Appdelegate or in view controller you need to ask permission -
func askForCameraPermission() {
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
//access granted
} else {
}
}
}
func askForPhotoLibraryPermission() {
let photos = PHPhotoLibrary.authorizationStatus()
if photos == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized{
//access granted
} else {}
})
}
}