I am working on a plugin for Mail.app. I want the plugin to add a button to the Mail toolbar. For this, I decided that the best approach would be to call a function to add this button to the MessageViewer initialization method (MessageViewer is the class for Mail.app FirstResponder). The code I'm modifying seems to work well:
+ (void) initialize { [super initialize];
However, when I try to do the same, it does not work:
// //Copy the method to the original MessageViewer swizzleSuccess &= [[self class] copyMethod:@selector(_specialInitMessageViewer:) fromClass:[self class] toClass:MessageViewer];
Here are the swizzling methods:
+ (BOOL)swizzleMethod:(SEL)origSel withMethod:(SEL)altSel inClass:(Class)cls { // For class (cls), swizzle the original selector with the new selector. //debug lines to try to figure out why swizzling is failing. // if (!cls || !origSel) { // NSLog(@"Something was null. Uh oh."); //} Method origMethod = class_getInstanceMethod(cls, origSel); if (!origMethod) { NSLog(@"Swizzler -- original method %@ not found for class %@", NSStringFromSelector(origSel), [cls className]); return NO; } //if (!altSel) NSLog(@"altSel null. :("); Method altMethod = class_getInstanceMethod(cls, altSel); if (!altMethod) { NSLog(@"Swizzler -- alternate method %@ not found for class %@", NSStringFromSelector(altSel), [cls className]); return NO; } method_exchangeImplementations(origMethod, altMethod); return YES; } + (BOOL) copyMethod:(SEL)sel fromClass:(Class)fromCls toClass:(Class)toCls { // copy a method from one class to another. Method method = class_getInstanceMethod(fromCls, sel); if (!method) { NSLog(@"copyMethod-- method %@ could not be found in class %@", NSStringFromSelector(sel), [fromCls className]); return NO; } class_addMethod(toCls, sel, class_getMethodImplementation(fromCls, sel), method_getTypeEncoding(method)); return YES; }
It seems that an error occurs in the call to class_getInstanceMethod, because I get an error message in the log. This happens for both the method inside my own class and the MessageViewer initialization method.
Are there any errors that I do not take into account here?
source share