我想在iOS中使用Hook委托(协议)方法,谁能告诉我该怎么做?
例如钩子方法applicationDidFinishLaunching:
我是这样做的,但它不起作用......
extern IMP original_UIApplication_applicationDidFinishLaunching;
extern void replaced_UIApplication_applicationDidFinishLaunching(UIApplication<UIApplicationDelegate> * self, SEL cmd, UIApplication* application);
MSHookMessageEx(objc_getMetaClass("UIApplication"),
@selector(applicationDidFinishLaunching:),
(IMP)replaced_UIApplication_applicationDidFinishLaunching,
(IMP *)&original_UIApplication_applicationDidFinishLaunching);
答案 0 :(得分:0)
applicationDidFinishLaunching是由应用程序DELEGATE实现的方法,而不是UIApplication本身。绝对没有理由需要在UIApplication上调整方法。只需在您的代理中实现这些方法。
对于要覆盖Apple提供的类中的方法的情况,您可以通过在其上创建类别来实现。添加的类别方法将通过相同的方法签名覆盖系统方法。
答案 1 :(得分:0)
你喋喋不休地交换了实现协议的类的方法。就像这样:
// Protocol Method Exchange
int numClasses = objc_getClassList(NULL, 0);
Class *list = (Class *)malloc(sizeof(Class) * numClasses);
objc_getClassList(list, numClasses);
for (int i = 0; i < numClasses; i++) {
if (class_conformsToProtocol(list[i], @protocol(TowstViewDelegate)) &&
class_getInstanceMethod(list[i], @selector(submit))) {
NSLog(@"%@ sumit have exchanged",NSStringFromClass(list[i]));
jm_swizzleSelector(list[i], @selector(submit), @selector(hk_submit));
}
}
free(list);
NSLog(@"Exchange END");