How to handle deselection event on PHPickerViewController?

41 views Asked by At

I'm trying to register a deselection event on PHPickerViewController but picker() function is being called only on selection event not on deselection event. Not even updateUIViewController() is being called when user makes the deselection.

Here is my code:

//--------------------------------------------------
// COORDINATOR CLASS
//--------------------------------------------------
class Coordinator: NSObject, PHPickerViewControllerDelegate
{
    //--------------------------------------------------
    // VARIABLES
    //--------------------------------------------------
    var parent: ImageChatPicker


    //--------------------------------------------------
    // METHODS
    //--------------------------------------------------
    // INIT
    init(_ parent: ImageChatPicker) {
        self.parent = parent
    }
    //--------------------------------------------------
    // PICKER
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        if results.isEmpty {
            parent.image = nil
            parent.showingImagePicker = false
        }
        // IF NOTHING HAS BEEN LOADED
        guard let provider = results.first?.itemProvider else { return }
        // IF IMAGE HAS BEEN LOADED
        if (provider.canLoadObject(ofClass: UIImage.self)) {
            provider.loadObject(ofClass: UIImage.self) { image, _ in
                DispatchQueue.main.async {
                    self.parent.image = image as? UIImage
                    if self.parent.image == nil {
                        let alertDialog = UIAlertController(title: "", message: "Unable to load image.\nTry different file.", preferredStyle: .alert)
                        alertDialog.addAction(UIAlertAction(title: "Ok", style: .default))
                        // Find the root view controller and present the alert
                        if let viewController = UIApplication.shared.windows.first?.rootViewController {
                            viewController.present(alertDialog, animated: true, completion: nil)
                        }
                    }
                }
            }
        }
    }
}

Does anybody know how to handle the deselection event?

0

There are 0 answers