Skip to content

Instantly share code, notes, and snippets.

@MP0w
Created May 6, 2014 12:31
Show Gist options
  • Save MP0w/16ff9e1a5fe776632ecc to your computer and use it in GitHub Desktop.
Save MP0w/16ff9e1a5fe776632ecc to your computer and use it in GitHub Desktop.
break delegates =)
// I will not tell you why I made this, or you would think I am mad...
// But may be useful in future
//
// Yes is the equivalent of super.. but it's not allowed in categories
+ (void)load{
method_exchangeImplementations(class_getInstanceMethod(self, @selector(setDelegate:)), class_getInstanceMethod(self, @selector(hooked_setDelegate:))); // hook the setDelegate to hook it (later)
}
- (void)hooked_setDelegate:(id<UIWebViewDelegate>)delegate{
[self hooked_setDelegate:delegate]; // it really call setDelegate: so is not a Loop, is the equivalent of [super setDelegate:delegate] if was allowed in categories...
Method hook=class_getInstanceMethod([UIWebView class], @selector(hooked_webViewDidFinishLoad:));
Class delegateClass=[delegate class];
class_addMethod(delegateClass,@selector(hooked_webViewDidFinishLoad:),method_getImplementation(hook),method_getTypeEncoding(hook)); // add the hook method that will be exchanged below with the original one
method_exchangeImplementations(class_getInstanceMethod(delegateClass, @selector(webViewDidFinishLoad:)), class_getInstanceMethod(delegateClass, @selector(hooked_webViewDidFinishLoad:))); // finally hooked!
}
- (void)hooked_webViewDidFinishLoad:(UIWebView *)webview{
[self hooked_webViewDidFinishLoad:webview]; // it really call webViewDidFinishLoad: so is not a Loop, is the equivalent of [super webViewDidFinishLoad:webview] if was allowed in categories...
// Do stuff now
//.....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment