我正在继承NSURLProtocol
来拦截HTTP请求。
以下是自定义NSURLProtocol
类。
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
if (NSOrderedSame != [request.URL.scheme caseInsensitiveCompare:@"http"] &&
NSOrderedSame != [request.URL.scheme caseInsensitiveCompare:@"https"]) {
return NO;
}
if ([NSURLProtocol propertyForKey:kURLProtocolRequestHandledKey inRequest:request] ) {
return NO;
}
return YES;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
NSMutableURLRequest *mutableReqeust = [request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:kURLProtocolRequestHandledKey inRequest:mutableReqeust];
return [mutableReqeust copy];
}
- (void)startLoading {
self.startDate = [NSDate date];
self.data = [NSMutableData data];
self.error = nil;
self.connection = [[NSURLConnection alloc] initWithRequest:[[self class] canonicalRequestForRequest:self.request] delegate:self startImmediately:YES];
}
- (void)stopLoading {
[self.connection cancel];
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[[self client] URLProtocol:self didFailWithError:error];
self.error = error;
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[self client] URLProtocol:self didReceiveAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[self client] URLProtocol:self didCancelAuthenticationChallenge:challenge];
}
#pragma mark - NSURLConnectionDataDelegate
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
if (response != nil){
_response = response;
NSMutableURLRequest *redirect = [request mutableCopy];
redirect.URL = request.URL;
[NSURLProtocol setProperty:@NO forKey:kURLProtocolRequestHandledKey inRequest:redirect];
[[self client] URLProtocol:self wasRedirectedToRequest:redirect redirectResponse:response];
return redirect;
}
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
_response = response;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[[self client] URLProtocol:self didLoadData:data];
[self.data appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return cachedResponse;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[self client] URLProtocolDidFinishLoading:self];
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
}
我添加一个UIWebView
作为子视图,然后加载一个URL http://ln.clientaccess.10086.cn/shop/optical/Appointment?channel=007&PHONE_NUM=18240235054&AREA_CODE=240&key=4A35774433BA79EB950EDE4B5C4D7121
,在控制器被解除后,即使我在webView被解除之前在webView上调用- stopLoading
,APP也会被冻结。
这是线程堆栈:
答案 0 :(得分:0)
我不认为你想在canonicalRequest中修改请求 - 不要管它。您确实希望在startLoading中修改它,并在对NSURLConnection的新调用中使用修改后的请求,以便您的协议在该调用期间不会再次处理它。
第二件事是重定向实现可能是错误的 - 该方法在两种情况下被调用;一旦发送请求(并且重定向为零);你希望在那种情况下(你做的)返回请求。第二个是当你真正获得重定向时;你想调用客户端(你是),但是你想要返回nil,以便让客户端实际处理重定向(否则非零返回可以指示已经处理了重定向)。
我不确定其中任何一个会导致问题,但它们是不同的。
我认为与我实施的唯一不同的是startImmediately:YES。这是默认值,因此不应成为问题。也许尝试避免缓存,看看是否有帮助,如果上述两者都没有。或者确保在dealloc中的连接上调用-cancel。
答案 1 :(得分:0)
使用startImmediately:NO
代替startImmediately:YES
将解决此问题。
if (currentRunLoop && [currentRunLoop currentMode]) {
self.connection = [[NSURLConnection alloc] initWithRequest:[[self class] canonicalRequestForRequest:self.request] delegate:self startImmediately:NO];
[self.connection scheduleInRunLoop:currentRunLoop forMode:[[NSRunLoop currentRunLoop] currentMode]];
[self.connection start];
} else {
self.connection = [[NSURLConnection alloc] initWithRequest:[[self class] canonicalRequestForRequest:self.request] delegate:self startImmediately:YES];
}
有线。有人请告诉我为什么?