我一直在编写一个 Mac 应用程序来检查每个 HTTP 请求,因此我使用 WebView 来加载请求。
问题是我似乎无法弄清楚如何获取每个资源项的内容。
我正在尝试通过 WebResourceLoadDelegate
方法获取资源的内容:
- (void)webView:(WebView *)sender resource:(id)identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
这似乎不起作用。
我也在查看 [dataSource data]
,但那只是为我提供了请求的 HTML 而不是资源项的数据。
关于如何获取资源内容的任何想法?
最佳答案
是的,这应该行得通,而且似乎曾几何时它行得通。您可以通过实现自定义 NSURLProtocol 来解决它。
首先,您需要确定您有兴趣拦截哪些请求(或所有请求,无论如何)。我在我的 WebResourceLoadDelegate 中执行此操作。如果我关心这个请求,我会用我自己的请求替换它,将自己作为属性(property)附加。稍后我将在 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 中,我将委托(delegate)移动到我的 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];
}
关于objective-c - 在 WebKit 中,如何获取资源的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3155359/