0

I've written a category for UIImagePickerController which I would only like to be accessed by one specific class. The category contains overrides for two private methods. The problem I'm having is that no matter how I try to define the category it seems that every class that instantiates a UIImagePickerController will override these methods. From my understanding this is the expected behaviour when creating a category, but is there any way around it?

I've tried defining the category in a separate .h/.m file as well as adding it directly to a separate class's implementation but neither seem to work. I've also tried creating an additional category which calls super for these methods but the original category still seems to take priority.

Any ideas or is this just not possible?

psobko
  • 1,548
  • 1
  • 14
  • 24
  • Categories really aren't a good option for overriding methods. It's possible to make that work but not only for a specific caller (unless your override has a way to determine what called it, and can then call the original method -- which requires figuring out a way to save its location). – mah Mar 11 '14 at 19:08
  • Why category? Why not subclass? – Cy-4AH Mar 11 '14 at 19:09
  • 1
    You *cannot* reliably override a method in a category. It is *undefined* whether the original method or the category method is called. – Martin R Mar 11 '14 at 19:11
  • @MartinR: The behavior is only undefined if the original method was itself defined in a category. Otherwise, the original is reliably clobbered (a problem in itself, of course). – jscs Mar 11 '14 at 19:29
  • 1
    @JoshCaswell: From https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html: If the name of a method declared in a category *is the same as a method in the original class*, [...] , the behavior is undefined as to which method implementation is used at runtime. – Martin R Mar 11 '14 at 19:34
  • Ah ok that's too bad but I expected as much. I tried a few different tricks with method swizzling as well but couldn't come up with anything. – psobko Mar 11 '14 at 19:43
  • @Cy-4AH Apple doesn't recommend subclassing UIImagePickerController: "The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. " – psobko Mar 11 '14 at 19:45
  • @psobko: Method swizzling also modifies the *class*. You cannot swizzle methods for a particular *instance* of the class (if that is what you are trying). – Martin R Mar 11 '14 at 19:51
  • Interesting, @Martinr; that's a stricter version of the warning than existed previously. Cf. bullet point one at http://stackoverflow.com/a/5272612/, from the old The Objective-C Programming Language doc. – jscs Mar 11 '14 at 20:48

0 Answers0