Skip to content

Instantly share code, notes, and snippets.

@Daniel1of1
Created February 18, 2014 13:09
Show Gist options
  • Save Daniel1of1/9070649 to your computer and use it in GitHub Desktop.
Save Daniel1of1/9070649 to your computer and use it in GitHub Desktop.
#import "NSURLSession+Swizzle.h"
#import <objc/runtime.h>
@implementation NSURLSession (Swizzle)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = self.class;
SEL originalSelector = @selector(downloadTaskWithURL:);
SEL swizzledSelector = @selector(xxx_downloadTaskWithURL:);
Method originalMethod = class_getInstanceMethod([NSURLSession sharedSession].class, originalSelector)
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
IMP swizzledImp = method_getImplementation(swizzledMethod);
IMP originalImp = method_getImplementation(originalMethod);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
swizzledImp,
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
originalImp,
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (NSURLSessionDownloadTask *)xxx_downloadTaskWithURL:(NSURL *)url
{
NSLog(@"%s", __PRETTY_FUNCTION__);
return [self xxx_downloadTaskWithURL:url];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment