我正在为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()的应用程序调用CAHTTPCookieStorage.highjack(),从ScreenSaverViewSubclass.init(...)调用我的屏幕保护程序。在预览模式下运行我的应用程序或屏幕保护程序(在系统首选项内),它运行正常,但当我作为一个正确的屏幕保护程序运行时,我可以看到消息"尝试高cookies的cookie。"但从来没有"饼干被劫持!!!!"。
任何想法可能会出错?也许线程有问题?是每个线程调整方法吗?
答案 0 :(得分:0)
对此的进一步调查证明,仅在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);
}