How is this handled in SwiftUI?
This extension send the alert.
Code Block func chooseImage() { let imagePicker = UIImagePickerController() imagePicker.delegate = self if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { PHPhotoLibrary.requestAuthorization({ (status) in switch status { case .authorized: DispatchQueue.main.async { imagePicker.sourceType = .photoLibrary imagePicker.allowsEditing = self.cropStatus self.present(imagePicker, animated: true) } case .notDetermined: DispatchQueue.main.async { imagePicker.sourceType = .photoLibrary imagePicker.allowsEditing = self.cropStatus self.present(imagePicker, animated: true) } case .restricted: self.view.sendConfirmationAlert(theTitle: "Photo Library Restricted", theMessage: "Photo Library access was previously denied. Please update your Settings to allow access.", buttonTitle: "OK") case .denied: let settingsAlert = UIAlertController(title: "Photo Library Access Denied", message: "Photo Library access was previously denied. Please update your Settings to allow access.", preferredStyle: .alert) let settingsAction = UIAlertAction(title: "Go to Settings", style: .default) { ( action ) in let settingsUrl = URL(string: UIApplication.openSettingsURLString) UIApplication.shared.open(settingsUrl!, options: [:]) } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) settingsAlert.addAction(settingsAction) settingsAlert.addAction(cancelAction) self.present(settingsAlert, animated: true) default: return } }) } }
This extension send the alert.
Code Block import UIKit import SystemConfiguration extension UIView { //MARK: - Send a confirmation alert func sendConfirmationAlert(theTitle: String, theMessage: String?, buttonTitle: String) { let theAlert = UIAlertController.init(title: theTitle, message: theMessage, preferredStyle: .alert) let theAction = UIAlertAction(title: buttonTitle, style: .default) theAlert.addAction(theAction) //present it on controller let window = UIApplication.shared.windows.first { $0.isKeyWindow } if let navController = window?.rootViewController as? UINavigationController { navController.present(theAlert, animated: true) } } }