Created
February 18, 2014 13:09
-
-
Save Daniel1of1/9070649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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