0
votes

I have a protocol ImagePickerPresentable:

protocol ImagePickerPresentable {
   var imagePicker: UIImagePickerController? { get set }
   func presentImagePicker(withSourceType: UIImagePickerControllerSourceType) throws
   func dismissImagePicker()
   weak var delegate: ImagePickerPresentableDelegate? { get set }
}

I have an extension with default implementation for both functions.

extension ImagePickerPresentable where Self: UIViewController

ImagePickerPresentableDelegate:

protocol ImagePickerPresentableDelegate: class {
    func imagePicker(imagePicker: UIImagePickerController, didFinishPickingImage image: UIImage?, withSuccess success: Bool)
}

I tried to extend ImagePickerPresentable for default implementation of imagePicker delegate functions:

extension ImagePickerPresentable where Self: UIImagePickerControllerDelegate & UINavigationControllerDelegate {

 func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissImagePicker()
}

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// No Callback
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        delegate?.imagePicker(imagePicker: picker, didFinishPickingImage: editedImage, withSuccess: true)
    } else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        delegate?.imagePicker(imagePicker: picker, didFinishPickingImage: originalImage, withSuccess: true)
    } else {
        delegate?.imagePicker(imagePicker: picker, didFinishPickingImage: nil, withSuccess: false)
    }
  }
}

In the view controller that adopts the protocol I assign self as imagePicker delegate, but ImagePickerPresentable doesn't get the callback when the imagePicker finished picking media.

If I implement the image picker delegate functions directly in the view controller, I do get the callback. I could set up a function in the protocol that handles the image picker delegate function output but is it possible to get the image picker delegate to use the default implementation of the ImagePickerPresentable delegate functions directly?

Since my View Controller conforms to ImagePickerPresentable, UIImagePickerControllerDelegate, and UINavigationControllerDelegate I thought the delegate functions in ImagePickerPresentable extension would be recognized by imagePicker delegate.

2

2 Answers

0
votes

You can use multicast delegate to achieve this.

0
votes

Two things to check here.

  1. Where is imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) being called? Is it being called at all?
  2. I'd add a guard statement to unwrap delegate, and return another kind of callback for an unsuccessfull unwrap.

Post more code, like an example controller, that you're using.