【问题标题】:Why would method swizzling be bypassed by the screen saver?为什么屏幕保护程序会绕过方法调配?
【发布时间】:2015-03-18 23:52:29
【问题描述】:

我正在为 Mac OS X 开发一个屏幕保护程序,我需要做一些方法调配,所以我做了一个小实验:

@implementation CAHTTPCookieStorage
+ (void) highjack {
    NSLog(@"Attempting to highjack cookies.");
    Class originalClass = [NSHTTPCookieStorage class];
    Method originalMeth = class_getClassMethod(originalClass, @selector(sharedHTTPCookieStorage));
    Method replacementMeth = class_getClassMethod([self class], @selector(patchedSharedHTTPCookieStorage));
    method_exchangeImplementations(originalMeth, replacementMeth);
}

+ (NSHTTPCookieStorage*) patchedSharedHTTPCookieStorage {
    NSLog(@"Cookies have been highjacked!!!!");
    return [CAHTTPCookieStorage patchedSharedHTTPCookieStorage];
}
@end

我从 AppDelegate.init() 的应用程序和 ScreenSaverViewSubclass.init(...) 的屏幕保护程序调用 CAHTTPCookieStorage.highjack()。在预览模式下(在系统偏好设置中)运行我的应用程序或屏幕保护程序时,它工作正常,但是当我将其作为适当的屏幕保护程序运行时,我可以看到消息“尝试劫持 cookie”。但绝不会“Cookie 已被劫持!!!!”。

有什么想法可能会出错吗?也许是线程的问题?每个线程的方法都在调配吗?

【问题讨论】:

    标签: macos cocoa screensaver


    【解决方案1】:

    对此的进一步调查证明,这仅在双显示器模式的 10.10 中是正确的,而在带有两个显示器的 10.9 或带有单个显示器的 10.10 中则不然。我不确定是什么原因造成的,但使用 +load 方法进行调配解决了它:

    + (void) load {
        NSLog(@"Attempting to highjack cookies.");
        Class originalClass = [NSHTTPCookieStorage class];
        Method originalMeth = class_getClassMethod(originalClass, @selector(sharedHTTPCookieStorage));
        Method replacementMeth = class_getClassMethod([self class], @selector(patchedSharedHTTPCookieStorage));
        method_exchangeImplementations(originalMeth, replacementMeth);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多