...
|
...
|
@@ -7,7 +7,243 @@ |
|
|
//
|
|
|
|
|
|
#import "NSURLSession+AutoTrack.h"
|
|
|
#import "YHSwizzle.h"
|
|
|
#import <objc/runtime.h>
|
|
|
#import <objc/message.h>
|
|
|
#import "YHEventReport.h"
|
|
|
#import "YHLog.h"
|
|
|
#import "YH_EventCollector.h"
|
|
|
|
|
|
|
|
|
@interface NSURLSession()
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
@implementation NSURLSession (AutoTrack)
|
|
|
|
|
|
#pragma mark - 属性
|
|
|
|
|
|
+(void)startTrack{
|
|
|
static dispatch_once_t onceToken;
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
@try {
|
|
|
|
|
|
[NSURLSessionTask startTrack];
|
|
|
|
|
|
SEL class_selectors[] = {
|
|
|
@selector(dataTaskWithRequest:completionHandler:),
|
|
|
@selector(dataTaskWithURL:completionHandler:),
|
|
|
@selector(uploadTaskWithRequest:fromData:completionHandler:),
|
|
|
@selector(uploadTaskWithRequest:fromFile:completionHandler:),
|
|
|
@selector(downloadTaskWithRequest:completionHandler:),
|
|
|
@selector(downloadTaskWithURL:completionHandler:),
|
|
|
@selector(downloadTaskWithResumeData:completionHandler:),
|
|
|
};
|
|
|
|
|
|
for (NSUInteger index = 0; index < sizeof(class_selectors) / sizeof(SEL); ++index) {
|
|
|
Class selfClass = object_getClass([self class]);
|
|
|
SEL oriSEL = class_selectors[index];
|
|
|
Method oriMethod = class_getClassMethod(selfClass, oriSEL);
|
|
|
SEL cusSEL = NSSelectorFromString([@"yher_" stringByAppendingString:NSStringFromSelector(oriSEL)]);
|
|
|
Method cusMethod = class_getClassMethod(selfClass, cusSEL);
|
|
|
|
|
|
BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
|
|
|
if (addSucc) {
|
|
|
class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
|
|
|
}else {
|
|
|
method_exchangeImplementations(oriMethod, cusMethod);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} @catch (NSException *exception) {
|
|
|
YHLog(@"%@ error: %@", self, exception);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
- (NSURLSessionDataTask *)yher_dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
__block NSURLSessionDataTask *task = [self yher_dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
if (completionHandler) {
|
|
|
completionHandler(data,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- (NSURLSessionDataTask *)yher_dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
__block NSURLSessionDataTask *task = [self yher_dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
if (completionHandler) {
|
|
|
completionHandler(data,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- (NSURLSessionUploadTask *)yher_uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
|
|
|
__block NSURLSessionUploadTask *task = [self yher_uploadTaskWithRequest:request fromFile:fileURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
if (completionHandler) {
|
|
|
completionHandler(data,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- (NSURLSessionUploadTask *)yher_uploadTaskWithRequest:(NSURLRequest *)request fromData:(nullable NSData *)bodyData completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
|
|
|
__block NSURLSessionUploadTask *task = [self yher_uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
if (completionHandler) {
|
|
|
completionHandler(data,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- (NSURLSessionDownloadTask *)yher_downloadTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
|
|
|
__block NSURLSessionDownloadTask *task = [self yher_downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
if (completionHandler) {
|
|
|
completionHandler(location,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- (NSURLSessionDownloadTask *)yher_downloadTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
|
|
|
__block NSURLSessionDownloadTask *task = [self yher_downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
if (completionHandler) {
|
|
|
completionHandler(location,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
- (NSURLSessionDownloadTask *)yher_downloadTaskWithResumeData:(NSData *)resumeData completionHandler:(void (^)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler
|
|
|
{
|
|
|
|
|
|
__block NSURLSessionDownloadTask *task = [self yher_downloadTaskWithResumeData:resumeData completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
if (completionHandler) {
|
|
|
completionHandler(location,response,error);
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
if (error) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusFailed];
|
|
|
}else{
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithSessionTask:task status:YHEventLoadStatusSuc];
|
|
|
}
|
|
|
}
|
|
|
}];
|
|
|
return task;
|
|
|
}
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation NSURLSessionTask (AutoTrack)
|
|
|
|
|
|
- (void)setYh_viewId:(NSString *)yh_viewId{
|
|
|
objc_setAssociatedObject(self,@selector(yh_viewId),yh_viewId,OBJC_ASSOCIATION_RETAIN);
|
|
|
}
|
|
|
|
|
|
- (NSString *)yh_viewId{
|
|
|
return objc_getAssociatedObject(self, @selector(yh_viewId));
|
|
|
}
|
|
|
|
|
|
+(void)startTrack{
|
|
|
static dispatch_once_t onceToken;
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
@try {
|
|
|
|
|
|
SEL class_selectors[] = {
|
|
|
@selector(resume),
|
|
|
};
|
|
|
|
|
|
for (NSUInteger index = 0; index < sizeof(class_selectors) / sizeof(SEL); ++index) {
|
|
|
Class selfClass = object_getClass([self class]);
|
|
|
SEL oriSEL = class_selectors[index];
|
|
|
Method oriMethod = class_getClassMethod(selfClass, oriSEL);
|
|
|
SEL cusSEL = NSSelectorFromString([@"yher_" stringByAppendingString:NSStringFromSelector(oriSEL)]);
|
|
|
Method cusMethod = class_getClassMethod(selfClass, cusSEL);
|
|
|
|
|
|
BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
|
|
|
if (addSucc) {
|
|
|
class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
|
|
|
}else {
|
|
|
method_exchangeImplementations(oriMethod, cusMethod);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
} @catch (NSException *exception) {
|
|
|
YHLog(@"%@ error: %@", self, exception);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
- (void)yher_resume
|
|
|
{
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventStartWithSession:self];
|
|
|
}
|
|
|
[self yher_resume];
|
|
|
}
|
|
|
|
|
|
@end
|
|
|
|
...
|
...
|
|