我一直在尝试调整 NSURLSession 类的 dataTaskWithURL 方法。这就是我尝试过的
+ (void)swizzleDataTaskWithRequest {
Class class = [self class];
SEL originalSelector = @selector(dataTaskWithRequest:completionHandler:);
SEL swizzledSelector = @selector(my_dataTaskWithRequest:completionHandler:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
- (NSURLSessionDataTask *)my_dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData * __nullable data, NSURLResponse * __nullable response, NSError * __nullable error))completionHandler{
NSLog(@"***************************");
return [self my_dataTaskWithURL:url completionHandler:completionHandler];
}
而这个 my_dataTaskWithURL 我想传递自己的完成处理程序,但我不知道如何创建它
提前致谢 !!