1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| + (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [NSURLConnection class];
SEL selector = @selector(initWithRequest:delegate:);
SEL swizzledSelector = @selector(test_initWithRequest:delegate:);
Method originalinitWithRequest = class_getInstanceMethod(class, selector);
NSURLConnection* (^swizzleBlock)(NSURLConnection *,SEL,NSURLRequest*,id )= ^(NSURLConnection *slf,SEL swizzledSelector,NSURLRequest *request,id delegate) {
return ((NSURLConnection*(*)(NSURLConnection*,SEL,NSURLRequest*,id))objc_msgSend)(slf,swizzledSelector,request,delegate);
};
IMP implementation = imp_implementationWithBlock(swizzleBlock);
class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalinitWithRequest));
Method newCOnnectionWithRequestClassMethod= class_getInstanceMethod(class, swizzledSelector);
method_exchangeImplementations(originalinitWithRequest, newConnectionWithRequestClassMethod);
});
} |