4

Mail.app 用のプラグインの作成に取り組んでいます。メールのツールバーにボタンを追加するプラグインが欲しいです。これを行うには、MessageViewer の初期化メソッド (MessageViewer は Mail.app の FirstResponder のクラス) にこのボタンを追加する関数を呼び出すことが最善の方法であると判断しました。私が変更しているコードはうまく機能しているようです:

+ (void) initialize
{  
[super initialize];

//  class_setSuperclass([self class], NSClassFromString(@"MVMailBundle"));
//  [ArchiveMailBundle registerBundle];


// Add a couple methods to the MessageViewer class.
Class MessageViewer = NSClassFromString(@"MessageViewer");

// swizzleSuccess should be NO if any of the following three calls fail
BOOL swizzleSuccess = YES;
swizzleSuccess &= [[self class] copyMethod:@selector(_specialValidateMenuItem:) 
                                 fromClass:[self class] 
                                   toClass:MessageViewer];
swizzleSuccess &= [[self class] copyMethod:@selector(unsubscribeSelectedMessages:) 
                                 fromClass:[self class] 
                                   toClass:MessageViewer];

ただし、同じことをしようとしてもうまくいきません:

//  //Copy the method to the original MessageViewer
swizzleSuccess &= [[self class] copyMethod:@selector(_specialInitMessageViewer:)
                                 fromClass:[self class]
                                   toClass:MessageViewer];  

スウィズルの方法は次のとおりです。

+ (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;
}

ログにエラーが表示されるため、class_getInstanceMethod の呼び出しに失敗しているようです。これは、自分のクラス内のメソッドと、MessageViewer の initialize メソッドの両方で発生します。

ここで考慮していない落とし穴はありますか?

4

2 に答える 2

9

クラスメソッドをスウィズルしたいのなら、なぜclass_getInstanceMethod()代わりに関数を使用しているのclass_getClassMethod()ですか?

于 2010-12-08T02:13:56.407 に答える
5

スウィズリングを手動で実装しようとする代わりに、JRSwizzleを使用する必要があります。1 つの注意点として、JRSwizzle の の実装+jr_swizzleClassMethod:withClassMethod:error:は単なるスタブですが、+jr_swizzleMethod:withMethod:error代わりにクラスのメタクラスを呼び出すことができます (たとえば、klass->isa代わりにklass(where klassis the class you're swizzling) を渡すだけです)。

于 2010-12-08T02:40:45.327 に答える