void SwizzInstanceMethod(Class origClass, Class replaceClass, SEL origSel, SEL replaceSel) { Method origMethod = class_getInstanceMethod(origClass, origSel); Method newMethod = class_getInstanceMethod(replaceClass, replaceSel); if(class_addMethod(origClass, origSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) class_replaceMethod(origClass, replaceSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); else method_exchangeImplementations(origMethod, newMethod); }
// ----------------------------------------------------------------------------- // Method Swizzling void makeSelectorChain(Class cls, SEL headSelector, SEL addOnSelector){ Method aspectMethod = class_getInstanceMethod(cls, addOnSelector); XAAssert(method_getImplementation(aspectMethod), "The aspect implementation should exist."); Method originalMethod = class_getInstanceMethod(cls, headSelector); XAAssert(method_getImplementation(originalMethod), "The original implementation should exist."); // Exchange implementations method_exchangeImplementations(originalMethod, aspectMethod); return; }
bool OBJC_EXCHANGE_NEWCLASS_METHOD_TEMPLATE(const char* orgClassStr,const char* orgSelector, const char* newClassStr,const char* newSelector) { Class orgClass = objc_getClass(orgClassStr); Class newClass = objc_getClass(newClassStr); if(!orgClass || !newClass) { return false; } SEL orgMethod = sel_registerName(orgSelector); SEL newMethod = sel_registerName(newSelector); Method newMethodIns = class_getInstanceMethod(newClass, newMethod); /*在旧的类添加类新方法后,那么只需要交换原有的方法即可*/ Method orgMethodIns = class_getInstanceMethod(orgClass, orgMethod); /*防止方法不存在*/ if(!orgMethodIns || !newMethodIns) { return false; } IMP newMethodIMP = method_getImplementation(newMethodIns); const char *newMethodStr = method_getTypeEncoding(newMethodIns); class_addMethod(orgClass, newMethod, newMethodIMP, newMethodStr); newMethodIns = class_getInstanceMethod(orgClass, newMethod); method_exchangeImplementations(orgMethodIns, newMethodIns); return true; }