Search code examples
iosuiimagepickercontroller

iOS: Detect or disable multiple image selection


We have a UIWebview with an image attachment control.

enter image description here

We the user taps the control an iOS action sheet pops up.

enter image description here

When the user taps 'Browse' or 'Photo Library' the user can select multiple images.

enter image description here

I then intercept the

- (void)imagePickerController: didFinishPickingMediaWithInfo: call with my own ImagePickerViewController that has been swizzled in.

So, my question: In didFinishPickingMediaWithInfo, is it possible to detect that multiple images have been selected? If not, is there a way to force the image selection on the iOS side to only allow one image?

If possible I would like to detect that multiple images have been selected, and then fall back to normal functionality (i.e. I won't swizzle).

I did examine the picker and info that is sent to didFinishPickingMediaWithInfo as arguments, but even though multiple images are selected, the info parameter only has info on one image. It keeps the first selection and discards the second, and the method is not called a second time.

Edit: I'm not using a 3rd party image picker.


Solution

  • The UIImagePickerController calls didFinishPickingMediaWithInfo and then calls didFinishPickingMultipleMediaWithInfo. It calls didFinishPickingMediaWithInfo first, I then check

    if info["UIImagePickerControllerReferenceURL"] == nil {
        // process the selected single image, we don't want to process multiple images
    } else {
        // do nothing
    }
    

    Then didFinishPickingMultipleMediaWithInfo is called, this time with the proper array, if the number of items in the array is greater than one, we use the normal methods and not the swizzled code, but if there is only one image, we process it with the swizzled methods as normal.

    So, we could not disable multiple image selection, but using didFinishPickingMultipleMediaWithInfo, we can detect when multiple images are selected and change how the app behaves in return.