Search code examples
iosobjective-cmethod-swizzling

How to swizzle into completion block of an Objective-C method?


Let's take this method as an example:

+ (void)animateWithDuration:(NSTimeInterval)duration
                 animations:(void (^)(void))animations
                 completion:(void (^)(BOOL finished))completion

It's simple enough to swizzle in a different or modified implementation of animatedWithDuration:animations:completion: method itself. What if I am instead interested in doing this for the completion block?


Solution

  • As others have pointed out, "swizzle" is used to refer to changing a method implementation, so you've the wrong term but that's not major.

    I'm guessing what you want to do is either: pass a different block to animatedWithDuration:animations:completion: than the caller supplies; or wrap the block the caller supplies in your own block - which amounts to much the same thing.

    If my guess is correct then you can swizzle the method replacing it by one which calls the original passing blocks of your choice, which could be wrappers around the blocks the caller supplied.

    HTH