...
|
...
|
@@ -16,11 +16,35 @@ |
|
|
|
|
|
@interface NSURLConnection()
|
|
|
|
|
|
@property(nullable,nonatomic,weak) id my_delegate;
|
|
|
|
|
|
@property (readonly, copy) NSURLResponse *my_response;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation NSURLConnection (AutoTrack)
|
|
|
|
|
|
#pragma mark - 属性
|
|
|
- (id)my_delegate
|
|
|
{
|
|
|
return objc_getAssociatedObject(self, _cmd);
|
|
|
}
|
|
|
|
|
|
- (void)setMy_delegate:(id)my_delegate
|
|
|
{
|
|
|
objc_setAssociatedObject(self, @selector(my_delegate), my_delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
}
|
|
|
|
|
|
- (id)my_response
|
|
|
{
|
|
|
return objc_getAssociatedObject(self, _cmd);
|
|
|
}
|
|
|
|
|
|
- (void)setMy_response:(id)my_response
|
|
|
{
|
|
|
objc_setAssociatedObject(self, @selector(my_response), my_response, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
|
}
|
|
|
|
|
|
|
|
|
+(void)startTrack{
|
|
|
static dispatch_once_t onceToken;
|
...
|
...
|
@@ -76,16 +100,40 @@ |
|
|
|
|
|
- (nullable instancetype)yher_initWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate startImmediately:(BOOL)startImmediately
|
|
|
{
|
|
|
return [self yher_initWithRequest:request delegate:delegate startImmediately:startImmediately];
|
|
|
[self setMy_delegate:delegate];
|
|
|
return [self yher_initWithRequest:request delegate:self startImmediately:startImmediately];
|
|
|
}
|
|
|
|
|
|
- (BOOL)respondsToSelector:(SEL)aSelector
|
|
|
{
|
|
|
if ([super respondsToSelector:aSelector])
|
|
|
return YES;
|
|
|
|
|
|
if ([self.my_delegate respondsToSelector:aSelector])
|
|
|
return YES;
|
|
|
|
|
|
return NO;
|
|
|
}
|
|
|
|
|
|
- (id)forwardingTargetForSelector:(SEL)aSelector
|
|
|
{
|
|
|
if ([self.my_delegate respondsToSelector:aSelector])
|
|
|
return self.my_delegate;
|
|
|
|
|
|
return [super forwardingTargetForSelector:aSelector];
|
|
|
}
|
|
|
|
|
|
- (nullable instancetype)yher_initWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate
|
|
|
{
|
|
|
return [self yher_initWithRequest:request delegate:delegate];
|
|
|
[self setMy_delegate:delegate];
|
|
|
return [self yher_initWithRequest:request delegate:self];
|
|
|
}
|
|
|
|
|
|
- (void)yher_start
|
|
|
{
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:self.currentRequest];
|
|
|
}
|
|
|
[self yher_start];
|
|
|
}
|
|
|
|
...
|
...
|
@@ -94,7 +142,7 @@ |
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:request];
|
|
|
}
|
|
|
|
|
|
|
|
|
NSData *data = [self yher_sendSynchronousRequest:request returningResponse:response error:error];
|
|
|
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
...
|
...
|
@@ -122,4 +170,38 @@ |
|
|
}];
|
|
|
}
|
|
|
|
|
|
#pragma mark - NSURLConnectionDelegate
|
|
|
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
|
|
connection.my_response = response;
|
|
|
|
|
|
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connection:didReceiveResponse:)]) {
|
|
|
[self.my_delegate connection:connection didReceiveResponse:response];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
|
|
|
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connection:didReceiveData:)]) {
|
|
|
[self.my_delegate connection:connection didReceiveData:data];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
|
|
|
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connectionDidFinishLoading:)]) {
|
|
|
[self.my_delegate connectionDidFinishLoading:connection];
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:connection.currentRequest response:connection.my_response];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
|
|
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connection:didFailWithError:)]) {
|
|
|
[self.my_delegate connection:connection didFailWithError:error];
|
|
|
}
|
|
|
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
|
|
|
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:connection.currentRequest response:nil];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@end |
...
|
...
|
|