Search code examples
iosuiwebviewobjective-c-runtimeuiresponder

Is it possible to substitute body of existing method in UIWebView without subclassing


I am interesting if it possible to substitute the method body of UIWebView in a runtime? I can't subclass UIWebView

For instance, I need to implement:

- (void) paste:(id)sender;
or
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender;

Solution

  • Have you tried out the runtime method method_setImplementation?

    Like so:

    static IMP originalPaste = NULL;
    
    void myPaste(id rcv, SEL cmd, id sender)
    {
        // Your implementation here
    }
    
    …
    {
        …
        Method m = class_getInstanceMethod([UIWebView class], @selector(paste:));
        originalPaste = method_setImplementation(m, myPaste);
        …
    }
    

    Using the originalPaste you can perform a super call within your implementation.