Authored by 孙凯

add session review by daixqoang

... ... @@ -28,34 +28,29 @@
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@try {
[NSURLSessionTask startTrack];
SEL class_selectors[] = {
SEL selectors[] = {
@selector(dataTaskWithRequest:completionHandler:),
@selector(dataTaskWithURL:completionHandler:),
@selector(uploadTaskWithRequest:fromData:completionHandler:),
@selector(uploadTaskWithRequest:fromFile:completionHandler:),
//@selector(dataTaskWithURL:completionHandler:),
//@selector(uploadTaskWithRequest:fromData:completionHandler:),
//@selector(uploadTaskWithRequest:fromFile:completionHandler:),
@selector(downloadTaskWithRequest:completionHandler:),
@selector(downloadTaskWithURL:completionHandler:),
@selector(downloadTaskWithResumeData: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);
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
SEL originalSelector = selectors[index];
SEL swizzledSelector = NSSelectorFromString([@"yher_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
NSError *error = NULL;
[[self class] yh_swizzleMethod:originalSelector
withMethod:swizzledSelector
error:&error];
if (error) {
YHLog(@"Failed to swizzle: on UIControl. Details: %@", error);
error = NULL;
}
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
}
... ... @@ -84,7 +79,7 @@
- (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);
}
... ... @@ -103,7 +98,7 @@
{
__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);
}
... ... @@ -122,7 +117,7 @@
{
__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);
}
... ... @@ -141,7 +136,7 @@
{
__block NSURLSessionDownloadTask *task = [self yher_downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(location,response,error);
}
... ... @@ -160,7 +155,7 @@
{
__block NSURLSessionDownloadTask *task = [self yher_downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(location,response,error);
}
... ... @@ -179,7 +174,7 @@
{
__block NSURLSessionDownloadTask *task = [self yher_downloadTaskWithResumeData:resumeData completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(location,response,error);
}
... ... @@ -211,24 +206,21 @@
dispatch_once(&onceToken, ^{
@try {
SEL class_selectors[] = {
SEL 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);
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
SEL originalSelector = selectors[index];
SEL swizzledSelector = NSSelectorFromString([@"yher_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
NSError *error = NULL;
[[self class] yh_swizzleMethod:originalSelector
withMethod:swizzledSelector
error:&error];
if (error) {
YHLog(@"Failed to swizzle: on UIControl. Details: %@", error);
error = NULL;
}
}
} @catch (NSException *exception) {
YHLog(@"%@ error: %@", self, exception);
... ...
... ... @@ -22,7 +22,6 @@
@try {
SEL selectors[] = {
@selector(addTarget:action:forControlEvents:),
@selector(beginTrackingWithTouch:withEvent:),
};
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
... ... @@ -94,21 +93,45 @@
#pragma mark - action
- (void)yher_UIControl_didTouch_cmdWithEvent:(UIControl*)sender
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventEndWithUIControl:sender];
[[YH_EventCollector sharedInstance] timeEventStartWithUIControl:self];
}
[self yher_UIControl_didTouch_cmdWithEvent:sender];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventEndWithUIControl:sender];
}
}
- (void)yher_UIControl_didTouch_cmd
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventEndWithUIControl:self];
[[YH_EventCollector sharedInstance] timeEventStartWithUIControl:self];
}
[self yher_UIControl_didTouch_cmd];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventEndWithUIControl:self];
}
}
- (void)yher_blockAction:(id)control forEvent:(UIEvent *)event{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventStartWithUIControl:self];
}
[self yher_blockAction:control forEvent:event];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventEndWithUIControl:self];
}
}
#pragma mark - hook API
... ... @@ -123,18 +146,13 @@
}else if ([[selectorAction componentsSeparatedByString:@":"]count]==2){
[UIControl yher_exchangeSelector:action target:target toSelector:@selector(yher_UIControl_didTouch_cmdWithEvent:)];
}
if ([selectorAction isEqualToString:@"blockAction:forEvent:"]) {
[UIControl yher_exchangeSelector:action target:target toSelector:@selector(yher_blockAction:forEvent:)];
}
}
[self yher_addTarget:target action:action forControlEvents:controlEvents];
}
- (BOOL)yher_beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
[[YH_EventCollector sharedInstance] timeEventStartWithUIControl:self];
}
return [self yher_beginTrackingWithTouch:touch withEvent:event];
}
@end
... ...