是的,这应该有效,而且似乎从前确实有效。你可以通过实现一个自定义的 NSURLProtocol 来解决它。
首先,您需要确定您有兴趣拦截哪些请求(或所有请求,无论如何)。我在我的 WebResourceLoadDelegate 中执行此操作。如果我关心请求,我会用我自己的请求替换它,将自己作为财产。稍后我将在 NSURLProtocol 中使用该属性。
- (NSURLRequest*) webView:(WebView*)sender
resource:(id)identifier
willSendRequest:(NSURLRequest*)request
redirectResponse:(NSURLResponse*)redirectResponse
fromDataSource:(WebDataSource*)dataSource
{
// Am I interested in this request?
if (/* figure out if you want it or not */) {
NSMutableURLRequest* newRequest = [[request mutableCopy] autorelease];
[NSURLProtocol setProperty:self forKey:@"MyApp" inRequest:newRequest];
return newRequest;
}
else {
// Not interested, let it go through normally
return request;
}
}
我的 NSURLProtocol 看起来像这样。
@interface MyURLProtocol : NSURLProtocol {
id _delegate;
NSURLConnection* _connection;
NSMutableData* _data;
}
@end
在我的 NSURLProtocol 子类中,我可以使用附加的属性来查看是否应该拦截响应。
+ (BOOL) canInitWithRequest:(NSURLRequest*)request
{
id delegate = [NSURLProtocol propertyForKey:@"MyApp" inRequest:request];
return (delegate != nil);
}
在 initWithRequest 中,我将委托移动到我的 NSURLProtocol 实例中,并从请求中删除该属性。这可以防止以后在我实际尝试加载此请求时出现无限循环。
- (id) initWithRequest:(NSURLRequest*)theRequest
cachedResponse:(NSCachedURLResponse*)cachedResponse
client:(id<NSURLProtocolClient>)client
{
// Move the delegate from the request to this instance
NSMutableURLRequest* req = (NSMutableURLRequest*)theRequest;
_delegate = [NSURLProtocol propertyForKey:@"MyApp" inRequest:req];
[NSURLProtocol removePropertyForKey:@"MyApp" inRequest:req];
// Complete my setup
self = [super initWithRequest:req cachedResponse:cachedResponse client:client];
if (self) {
_data = [[NSMutableData data] retain];
}
return self;
}
剩下的是股票 URL 加载的东西。
- (void) startLoading
{
_connection = [[NSURLConnection connectionWithRequest:[self request] delegate:self] retain];
}
- (void) stopLoading
{
[_connection cancel];
}
- (void)connection:(NSURLConnection*)conn didReceiveResponse:(NSURLResponse*)response
{
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:[[self request] cachePolicy]];
[_data setLength:0];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[[self client] URLProtocol:self didLoadData:data];
[_data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)conn
{
[[self client] URLProtocolDidFinishLoading:self];
// Forward the response to your delegate however you like
if (_delegate && [_delegate respondsToSelector:@selector(...)]) {
[_delegate ... withRequest:[self request] withData:_data];
}
}
- (NSURLRequest*)connection:(NSURLConnection*)connection willSendRequest:(NSURLRequest*)theRequest redirectResponse:(NSURLResponse*)redirectResponse
{
return theRequest;
}
- (void)connection:(NSURLConnection*)conn didFailWithError:(NSError*)error
{
[[self client] URLProtocol:self didFailWithError:error];
}