Skip to content

Commit 85904e6

Browse files
Roc.TianRoc.Tian
Roc.Tian
authored and
Roc.Tian
committed
修复捕获不到get方法的问题
1 parent c92e1ea commit 85904e6

File tree

5 files changed

+148
-84
lines changed

5 files changed

+148
-84
lines changed

TPFDebugTool.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'TPFDebugTool'
11-
s.version = '0.1.7'
11+
s.version = '0.1.8'
1212
s.summary = 'TPFDebugTool is a debugging tool to monitor the network, view the log, collect crash log'
1313

1414
# This description is used to generate tags and improve search results.

TPFDebugTool/Classes/JxbCrashVC.m

+17-9
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ - (void)viewDidLoad {
2323
[super viewDidLoad];
2424
[self.navigationItem setTitle:@"Crash"];
2525

26-
UIButton *btnclose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
27-
btnclose.titleLabel.font = [UIFont systemFontOfSize:13];
28-
[btnclose setTitle:@"关闭" forState:UIControlStateNormal];
29-
[btnclose addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
30-
[btnclose setTitleColor:[JxbDebugTool shareInstance].mainColor forState:UIControlStateNormal];
31-
32-
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithCustomView:btnclose];
33-
self.navigationItem.leftBarButtonItem = btnleft;
3426

3527
self.listData = [[JxbCrashHelper sharedInstance] crashLogs];
3628

@@ -43,7 +35,23 @@ - (void)viewDidLoad {
4335
[self.view addSubview:self.tableView];
4436

4537
}
46-
38+
-(void)viewWillAppear:(BOOL)animated{
39+
[super viewWillAppear:animated];
40+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
41+
[self setLeftBarButtonItem];
42+
});
43+
}
44+
-(void)setLeftBarButtonItem{
45+
46+
UIButton *btnclose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
47+
btnclose.titleLabel.font = [UIFont systemFontOfSize:13];
48+
[btnclose setTitle:@"关闭" forState:UIControlStateNormal];
49+
[btnclose addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
50+
[btnclose setTitleColor:[JxbDebugTool shareInstance].mainColor forState:UIControlStateNormal];
51+
52+
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithCustomView:btnclose];
53+
self.navigationItem.leftBarButtonItem = btnleft;
54+
}
4755
- (void)dismissViewController {
4856
// [self dismissViewControllerAnimated:YES completion:nil];
4957
[[JxbDebugTool shareInstance] showDebug];

TPFDebugTool/Classes/JxbHttpVC.m

+17-10
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ - (void)dealloc {
2525
- (void)viewDidLoad {
2626
[super viewDidLoad];
2727
[self.navigationItem setTitle:@"Http"];
28-
29-
UIButton *btnclose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
30-
btnclose.titleLabel.font = [UIFont systemFontOfSize:13];
31-
[btnclose setTitle:@"关闭" forState:UIControlStateNormal];
32-
[btnclose addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
33-
[btnclose setTitleColor:[JxbDebugTool shareInstance].mainColor forState:UIControlStateNormal];
34-
35-
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithCustomView:btnclose];
36-
self.navigationItem.leftBarButtonItem = btnleft;
3728

3829
UIButton *btnclear = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
3930
btnclear.titleLabel.font = [UIFont systemFontOfSize:13];
@@ -57,7 +48,23 @@ - (void)viewDidLoad {
5748

5849
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadHttp) name:kNotifyKeyReloadHttp object:nil];
5950
}
60-
51+
-(void)viewWillAppear:(BOOL)animated{
52+
[super viewWillAppear:animated];
53+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
54+
[self setLeftBarButtonItem];
55+
});
56+
}
57+
-(void)setLeftBarButtonItem{
58+
59+
UIButton *btnclose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
60+
btnclose.titleLabel.font = [UIFont systemFontOfSize:13];
61+
[btnclose setTitle:@"关闭" forState:UIControlStateNormal];
62+
[btnclose addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
63+
[btnclose setTitleColor:[JxbDebugTool shareInstance].mainColor forState:UIControlStateNormal];
64+
65+
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithCustomView:btnclose];
66+
self.navigationItem.leftBarButtonItem = btnleft;
67+
}
6168
- (void)dismissViewController {
6269
// [self dismissViewControllerAnimated:YES completion:nil];
6370
[[JxbDebugTool shareInstance] showDebug];

TPFDebugTool/Classes/JxbLogVC.m

+17-10
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ - (void)viewDidLoad {
9191
[self.view addSubview:txt];
9292

9393

94-
UIButton *btnclose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
95-
btnclose.titleLabel.font = [UIFont systemFontOfSize:13];
96-
[btnclose setTitle:@"关闭" forState:UIControlStateNormal];
97-
[btnclose addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
98-
[btnclose setTitleColor:[JxbDebugTool shareInstance].mainColor forState:UIControlStateNormal];
99-
100-
UIBarButtonItem *barclose = [[UIBarButtonItem alloc] initWithCustomView:btnclose];
101-
self.navigationItem.leftBarButtonItem = barclose;
102-
10394
UIButton *btnrefresh = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
10495
btnrefresh.titleLabel.font = [UIFont systemFontOfSize:13];
10596
[btnrefresh setTitle:@"刷新" forState:UIControlStateNormal];
@@ -112,7 +103,23 @@ - (void)viewDidLoad {
112103

113104
[self loadLogs];
114105
}
115-
106+
-(void)viewWillAppear:(BOOL)animated{
107+
[super viewWillAppear:animated];
108+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
109+
[self setLeftBarButtonItem];
110+
});
111+
}
112+
-(void)setLeftBarButtonItem{
113+
114+
UIButton *btnclose = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
115+
btnclose.titleLabel.font = [UIFont systemFontOfSize:13];
116+
[btnclose setTitle:@"关闭" forState:UIControlStateNormal];
117+
[btnclose addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
118+
[btnclose setTitleColor:[JxbDebugTool shareInstance].mainColor forState:UIControlStateNormal];
119+
120+
UIBarButtonItem *btnleft = [[UIBarButtonItem alloc] initWithCustomView:btnclose];
121+
self.navigationItem.leftBarButtonItem = btnleft;
122+
}
116123
- (void)dismissViewController {
117124
// [self dismissViewControllerAnimated:YES completion:nil];
118125
[[JxbDebugTool shareInstance] showDebug];

TPFDebugTool/Classes/NSURLSessionTask+Swizzling.m

+96-54
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
@implementation NSURLSession (Swizzling)
2121

2222
+ (void)load {
23-
23+
2424
Method oriMethod = class_getInstanceMethod([NSURLSession class], @selector(dataTaskWithRequest:));
2525
Method newMethod = class_getInstanceMethod([NSURLSession class], @selector(dataTaskWithRequest_swizzling:));
2626
method_exchangeImplementations(oriMethod, newMethod);
@@ -81,29 +81,38 @@ + (void)swizzling_TaskWillPerformHTTPRedirectionIntoDelegateClass:(Class)cls {
8181

8282
+ (void)swizzling_TaskDidSendBodyDataIntoDelegateClass:(Class)cls {
8383
SEL selector = @selector(URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:);
84-
// SEL swizzledSelector = @selector(URLSession_swizzling:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:);
85-
// Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
86-
//
87-
// struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
88-
// [self replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription];
89-
84+
// SEL swizzledSelector = @selector(URLSession_swizzling:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:);
85+
// Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
86+
//
87+
// struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
88+
// [self replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription];
89+
9090
NSError *error;
9191

9292
[cls aspect_hookSelector:selector
93-
withOptions:AspectPositionBefore
94-
usingBlock:^(id <AspectInfo> aspectInfo,NSURLSession *session,NSURLSessionTask *task){
95-
96-
[self addRequestModel:session task:task];
97-
98-
}
99-
error:&error];
93+
withOptions:AspectPositionBefore
94+
usingBlock:^(id <AspectInfo> aspectInfo,NSURLSession *session,NSURLSessionTask *task){
95+
96+
[self addRequestModel:session task:task];
97+
98+
}
99+
error:&error];
100+
100101
}
101102
+(void)addRequestModel:(NSURLSession *)session task:(NSURLSessionTask *)task {
102103

103104
NSURLRequest* req = task.originalRequest;
104-
req.requestId = [[NSUUID UUID] UUIDString];
105-
req.startTime = @([[NSDate date] timeIntervalSince1970]);
106105

106+
if ([[JxbHttpDatasource shareInstance] arrRequestContainObject:req.requestId])
107+
return;
108+
109+
if (![self canHandle:req])
110+
return;
111+
112+
[self createModel:req statusCode:@"100"];
113+
114+
}
115+
+(BOOL)canHandle:(NSURLRequest *)req{
107116
BOOL canHandle = YES;
108117
if ([[JxbDebugTool shareInstance] arrOnlyHosts].count > 0) {
109118
canHandle = NO;
@@ -115,16 +124,20 @@ +(void)addRequestModel:(NSURLSession *)session task:(NSURLSessionTask *)task {
115124
}
116125
}
117126
}
118-
if (!canHandle)
119-
return;
127+
return canHandle;
128+
}
129+
+(void)createModel:(NSURLRequest *)req statusCode:(NSString *)statusCode{
130+
131+
req.requestId = [[NSUUID UUID] UUIDString];
132+
req.startTime = @([[NSDate date] timeIntervalSince1970]);
120133

121134
JxbHttpModel* model = [[JxbHttpModel alloc] init];
122135
model.requestId = req.requestId;
123136
model.url = req.URL;
124137
model.method = req.HTTPMethod;
125138
model.requestAllHTTPHeaderFields = req.allHTTPHeaderFields;
126139
model.startTime = [NSString stringWithFormat:@"%fs",req.startTime.doubleValue];
127-
model.statusCode = @"100";
140+
model.statusCode = statusCode;
128141
if (req.HTTPBody) {
129142
NSData* data = req.HTTPBody;
130143
if ([[JxbDebugTool shareInstance] isHttpRequestEncrypt]) {
@@ -137,7 +150,6 @@ +(void)addRequestModel:(NSURLSession *)session task:(NSURLSessionTask *)task {
137150

138151
[[JxbHttpDatasource shareInstance] addHttpRequset:model];
139152
[[NSNotificationCenter defaultCenter] postNotificationName:kNotifyKeyReloadHttp object:nil];
140-
141153
}
142154
+ (void)swizzling_TaskDidReceiveChallengeIntoDelegateClass:(Class)cls {
143155
SEL selector = @selector(URLSession:task:didReceiveChallenge:completionHandler:);
@@ -159,11 +171,11 @@ + (void)swizzling_TaskNeedNewBodyStreamIntoDelegateClass:(Class)cls {
159171

160172
+ (void)swizzling_TaskDidCompleteWithErrorIntoDelegateClass:(Class)cls {
161173
SEL selector = @selector(URLSession:task:didCompleteWithError:);
162-
// SEL swizzledSelector = @selector(URLSession_swizzling:task:didCompleteWithError:);
163-
// Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
164-
//
165-
// struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
166-
// [self replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription];
174+
// SEL swizzledSelector = @selector(URLSession_swizzling:task:didCompleteWithError:);
175+
// Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
176+
//
177+
// struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
178+
// [self replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription];
167179

168180
NSError *error;
169181

@@ -183,9 +195,9 @@ - (void)URLSession_swizzling:(NSURLSession *)session task:(NSURLSessionTask *)ta
183195
}
184196

185197
- (void)URLSession_swizzling:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
186-
187198

188-
// [self URLSession_swizzling:session task:task didSendBodyData:bytesSent totalBytesSent:totalBytesSent totalBytesExpectedToSend:totalBytesExpectedToSend];
199+
200+
// [self URLSession_swizzling:session task:task didSendBodyData:bytesSent totalBytesSent:totalBytesSent totalBytesExpectedToSend:totalBytesExpectedToSend];
189201
}
190202

191203
- (void)URLSession_swizzling:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
@@ -197,34 +209,63 @@ - (void)URLSession_swizzling:(NSURLSession *)session task:(NSURLSessionTask *)ta
197209
}
198210

199211
+(void)URLSession_swizzling:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
200-
// [self URLSession_swizzling:session task:task didCompleteWithError:error];
201-
202-
NSURLRequest* req = task.originalRequest;
203-
if (![[JxbHttpDatasource shareInstance] arrRequestContainObject:req.requestId])
204-
return;
212+
// [self URLSession_swizzling:session task:task didCompleteWithError:error];
205213

214+
NSURLRequest* req = task.originalRequest;
206215
NSURLResponse* resp = task.response;
207-
208-
__block JxbHttpModel* model;
209-
NSMutableArray *httpArray = [[[JxbHttpDatasource shareInstance] httpArray] mutableCopy];
210-
[httpArray enumerateObjectsUsingBlock:^(JxbHttpModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
216+
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)resp;
217+
NSString *statusCode = [NSString stringWithFormat:@"%d",(int)httpResponse.statusCode];
218+
219+
if ([[JxbHttpDatasource shareInstance] arrRequestContainObject:req.requestId]){
211220

212-
if([obj.requestId isEqualToString:req.requestId]){
213-
model = obj;
214-
*stop = YES;
221+
__block JxbHttpModel* model;
222+
NSMutableArray *httpArray = [[[JxbHttpDatasource shareInstance] httpArray] mutableCopy];
223+
[httpArray enumerateObjectsUsingBlock:^(JxbHttpModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
224+
225+
if([obj.requestId isEqualToString:req.requestId]){
226+
model = obj;
227+
*stop = YES;
228+
}
229+
}];
230+
231+
model.statusCode = statusCode;
232+
model.responseData = task.responseDatas;
233+
model.responseAllHTTPHeaderFields = httpResponse.allHeaderFields;
234+
model.mineType = httpResponse.MIMEType;
235+
model.isImage = [resp.MIMEType rangeOfString:@"image"].location != NSNotFound;
236+
model.totalDuration = [NSString stringWithFormat:@"%fs",[[NSDate date] timeIntervalSince1970] - model.startTime.doubleValue];
237+
238+
[[NSNotificationCenter defaultCenter] postNotificationName:kNotifyKeyReloadHttp object:model];
239+
}
240+
else if ([self canHandle:req]){
241+
242+
JxbHttpModel* model = [[JxbHttpModel alloc] init];
243+
model.requestId = req.requestId;
244+
model.url = req.URL;
245+
model.method = req.HTTPMethod;
246+
model.requestAllHTTPHeaderFields = req.allHTTPHeaderFields;
247+
model.startTime = [NSString stringWithFormat:@"%fs",[[NSDate date] timeIntervalSince1970]];
248+
model.statusCode = statusCode;
249+
if (req.HTTPBody) {
250+
NSData* data = req.HTTPBody;
251+
if ([[JxbDebugTool shareInstance] isHttpRequestEncrypt]) {
252+
if ([[JxbDebugTool shareInstance] delegate] && [[JxbDebugTool shareInstance].delegate respondsToSelector:@selector(decryptJson:)]) {
253+
data = [[JxbDebugTool shareInstance].delegate decryptJson:req.HTTPBody];
254+
}
255+
}
256+
model.requestBody = [JxbHttpDatasource prettyJSONStringFromData:data error:nil];
215257
}
216-
}];
217-
218-
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)resp;
219-
model.statusCode = [NSString stringWithFormat:@"%d",(int)httpResponse.statusCode];
220-
model.responseData = task.responseDatas;
221-
model.responseAllHTTPHeaderFields = httpResponse.allHeaderFields;
222-
model.mineType = httpResponse.MIMEType;
223-
model.isImage = [resp.MIMEType rangeOfString:@"image"].location != NSNotFound;
224-
NSString *mimeType = resp.MIMEType;
225-
model.totalDuration = [NSString stringWithFormat:@"%fs",[[NSDate date] timeIntervalSince1970] - model.startTime.doubleValue];
226-
227-
[[NSNotificationCenter defaultCenter] postNotificationName:kNotifyKeyReloadHttp object:model];
258+
259+
model.responseData = task.responseDatas;
260+
model.responseAllHTTPHeaderFields = httpResponse.allHeaderFields;
261+
model.mineType = httpResponse.MIMEType;
262+
model.isImage = [resp.MIMEType rangeOfString:@"image"].location != NSNotFound;
263+
model.totalDuration = [NSString stringWithFormat:@"%fs",[[NSDate date] timeIntervalSince1970] - model.startTime.doubleValue];
264+
265+
266+
[[JxbHttpDatasource shareInstance] addHttpRequset:model];
267+
[[NSNotificationCenter defaultCenter] postNotificationName:kNotifyKeyReloadHttp object:nil];
268+
}
228269

229270
}
230271

@@ -235,7 +276,7 @@ + (void)swizzling_selectors_data:(Class)cls {
235276
[self swizzling_TaskDidReceiveDataIntoDelegateClass:cls];
236277
[self swizzling_TaskDidBecomeDownloadTaskIntoDelegateClass:cls];
237278
[self swizzling_TaskDidBecomeStreamTaskIntoDelegateClass:cls];
238-
// [self swizzling_TaskWillCacheResponseIntoDelegateClass:cls];
279+
// [self swizzling_TaskWillCacheResponseIntoDelegateClass:cls];
239280
}
240281

241282
+ (void)swizzling_TaskDidReceiveResponseIntoDelegateClass:(Class)cls {
@@ -251,7 +292,7 @@ + (void)swizzling_TaskDidReceiveDataIntoDelegateClass:(Class)cls {
251292
SEL selector = @selector(URLSession:dataTask:didReceiveData:);
252293
SEL swizzledSelector = @selector(URLSession_swizzling:dataTask:didReceiveData:);
253294
Protocol *protocol = @protocol(NSURLSessionDataDelegate);
254-
295+
255296
struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
256297
[self replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription];
257298
}
@@ -291,7 +332,7 @@ + (void)replaceImplementationOfSelector:(SEL)selector withSelector:(SEL)swizzled
291332
Method newMethod = class_getInstanceMethod(cls, swizzledSelector);
292333
method_exchangeImplementations(oldMethod, newMethod);
293334
} else { // 没有实例方法的话,就不要添加
294-
// class_addMethod(cls, selector, implementation, methodDescription.types);
335+
// class_addMethod(cls, selector, implementation, methodDescription.types);
295336
}
296337
}
297338

@@ -312,6 +353,7 @@ - (void)URLSession_swizzling:(NSURLSession *)session dataTask:(NSURLSessionDataT
312353
}
313354
if ([dataTask.taskDataIdentify isEqualToString:NSStringFromClass([self class])])
314355
[dataTask.responseDatas appendData:data];
356+
315357
[self URLSession_swizzling:session dataTask:dataTask didReceiveData:data];
316358
}
317359

0 commit comments

Comments
 (0)