Hello everybody,
I have recently ran into a problem with my app the I am not being able to solve by myself.
I have implemented the new PHPicker. It is the best option to handle the new limited case where the app can only access photos approved by the user.
After having this completed I implemented the .requestAuthorization part of the application.
This is where my problem is.
At the moment, I have this code:
First I check to see if the status is already determined. If it is not I call PHPhotoLibrary.requestAuthorization. Here things start to get really weird.
When I press "Select Photos" on my iPhone the picker to select the photos comes up and after I have selected the photos my code enters in the .authorized case and it prints "Full access".
However, after that when I try to open the picker it lands on the .limited case but the picker displays all the images in the camera roll and not only those I have selected.
Here is the code for my picker.
I am really struggling and all help is appreciated. Thanks a lot!
I have recently ran into a problem with my app the I am not being able to solve by myself.
I have implemented the new PHPicker. It is the best option to handle the new limited case where the app can only access photos approved by the user.
After having this completed I implemented the .requestAuthorization part of the application.
This is where my problem is.
At the moment, I have this code:
Code Block if #available(iOS 14, *) { let accessLevel: PHAccessLevel = .readWrite let status = PHPhotoLibrary.authorizationStatus(for: accessLevel) switch status { case .authorized: self.imagePickerIsPresented.toggle() case .limited: print("Limited access - show picker.") self.imagePickerIsPresented.toggle() case .denied: self.showPhotosAccessDeniedAlert.toggle() case .notDetermined: PHPhotoLibrary.requestAuthorization { newStatus in switch newStatus { case .authorized: print("Full access.") case .limited: print("Limited access.") break case .denied: break default: break } } default: break } }
First I check to see if the status is already determined. If it is not I call PHPhotoLibrary.requestAuthorization. Here things start to get really weird.
When I press "Select Photos" on my iPhone the picker to select the photos comes up and after I have selected the photos my code enters in the .authorized case and it prints "Full access".
However, after that when I try to open the picker it lands on the .limited case but the picker displays all the images in the camera roll and not only those I have selected.
Here is the code for my picker.
Code Block import SwiftUI import PhotosUI @available(iOS 14, *) struct ImagePicker: UIViewControllerRepresentable { @Binding var imageToImport: UIImage @Binding var isPresented: Bool @Binding var imageWasImported: Bool func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> some UIViewController { var configuration = PHPickerConfiguration() configuration.filter = .images configuration.selectionLimit = 1 let imagePicker = PHPickerViewController(configuration: configuration) imagePicker.delegate = context.coordinator return imagePicker } func updateUIViewController(_ uiViewController: ImagePicker.UIViewControllerType, context: UIViewControllerRepresentableContext<ImagePicker>) {} func makeCoordinator() -> ImagePicker.Coordinator { return Coordinator(parent: self) } class Coordinator: NSObject, PHPickerViewControllerDelegate { var parent: ImagePicker init(parent: ImagePicker) { self.parent = parent } func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true) if results.count != 1 { return } if let image = results.first { print("Aqui") if image.itemProvider.canLoadObject(ofClass: UIImage.self) { image.itemProvider.loadObject(ofClass: UIImage.self) { image, error in if let image = image { print("Importou imagem") self.parent.imageToImport = image as! UIImage self.parent.imageWasImported.toggle() } } } } self.parent.isPresented.toggle() } } }
I am really struggling and all help is appreciated. Thanks a lot!