ios - dynamically created method not being called -
after dynamically creating method in appdelegate using class_addmethod
class, never gets called when should.
the method create on runtime is:
-(bool)application:(uiapplication *)application openurl:(nsurl *)url { //do something... return yes; }
the code used create previous method dynamically is:
{ uiapplication* app = [[uiapplication sharedapplication] delegate]; class_addmethod([app class], @selector(application:openurl:), (imp)mymethodimp, @"b@:"); }
along imp
function:
bool mymethodimp(id self, sel _cmd, uiapplication *application, nsurl *url) { [self application: application openurl:url]; //do something... return yes; }
i have verified indeed created on app delegate using class_getinstancemethod
method
am missing something?
@bbum right,uiapplication
cache delegate dones not respond method, can this
@interface uiapplication (mbshare) @end @implementation uiapplication (mbshare) + (void) load { static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ sel originalselector = @selector(setdelegate:); sel swizzledselector = @selector(mb_setdelegate:); method originalmethod = class_getinstancemethod(self, originalselector); method swizzledmethod = class_getinstancemethod(self, swizzledselector); bool didaddmethod = class_addmethod(self, originalselector, method_getimplementation(swizzledmethod), method_gettypeencoding(swizzledmethod)); if (didaddmethod) { class_replacemethod(self, swizzledselector, method_getimplementation(originalmethod), method_gettypeencoding(originalmethod)); } else { method_exchangeimplementations(originalmethod, swizzledmethod); } }); } - (void) mb_setdelegate: (id) delegate { //hook method here [[mbshareapi shareinstance] hookappdelegate: delegate]; [self mb_setdelegate: delegate]; } @end
more detail see:https://github.com/baoge2012/mbshare/blob/master/mbshare/mbshareapi.m
Comments
Post a Comment