Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 拦截来自WebView Flash插件的web请求_Cocoa_Webkit - Fatal编程技术网

Cocoa 拦截来自WebView Flash插件的web请求

Cocoa 拦截来自WebView Flash插件的web请求,cocoa,webkit,Cocoa,Webkit,我有一个桌面浏览器应用程序,它使用WebView来托管Flash插件。Flash插件定期向外部网站请求新数据,然后将其绘制为精美的图形 我想截取这些web请求并获取数据(这样我就可以通过咆哮来显示数据,而不是在桌面上打开窗口)。但据我所知,Flash发出的请求不会被普通的WebView代理接收 还有别的地方可以挂钩吗?我尝试通过[NSURLCache setSharedURLCache]安装自定义NSURLCache,但从未调用过。我还尝试了方法swizzling一些其他类(比如NSCached

我有一个桌面浏览器应用程序,它使用WebView来托管Flash插件。Flash插件定期向外部网站请求新数据,然后将其绘制为精美的图形

我想截取这些web请求并获取数据(这样我就可以通过咆哮来显示数据,而不是在桌面上打开窗口)。但据我所知,Flash发出的请求不会被普通的WebView代理接收


还有别的地方可以挂钩吗?我尝试通过[NSURLCache setSharedURLCache]安装自定义NSURLCache,但从未调用过。我还尝试了方法swizzling一些其他类(比如NSCachedURLResponse),但找不到进入的方法。有什么想法吗?非常感谢

没人回答这个问题,这其实很简单。创建NSURLProtocol的子类,然后调用registerClass开始拦截

[NSURLProtocol registerClass:[MyCustomURLProtocol class]];
下面是子类的重要部分:

#define REQUEST_HEADER_TAG  @"x-mycustomurl-intercept"

+ (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
{
    // Check for the custom header on the request to break the 
    // infinite loop created by the [startLoading] below.
    if ([theRequest valueForHTTPHeaderField:REQUEST_HEADER_TAG]) {
        return NO;
    }

    if ([theRequest.URL.scheme caseInsensitiveCompare:@"http"] == NSOrderedSame) {
        return YES;
    }
    return NO;
}

+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)theRequest
{
    return theRequest;
}

- (id)initWithRequest:(NSURLRequest*)theRequest
       cachedResponse:(NSCachedURLResponse*)cachedResponse
               client:(id<NSURLProtocolClient>)client
{
    // Add a custom header on the request to break the 
    // infinite loop created by the [startLoading] below.
    NSMutableURLRequest* newRequest = [theRequest mutableCopy];
    [newRequest setValue:@"" forHTTPHeaderField:REQUEST_HEADER_TAG];

    // Now continue the process with this "tagged" request
    self = [super initWithRequest:theRequest 
                   cachedResponse:cachedResponse 
                           client:client];
    if (self) {
       // capture the data received 
       [self setRequest:newRequest];
       receivedData = [[NSMutableData data] retain];
    }

    [newRequest release];
    return self;
}

- (void)dealloc
{
    [connection release];
    [request release];
    [receivedData release];
    [super dealloc];
}


- (void)startLoading
{
    // Load the data off the web as usual, but set myself up as the delegate 
    // so I can intercept the response data as it comes in.
    [self setConnection:[NSURLConnection connectionWithRequest:request delegate:self]];
}


- (void)stopLoading 
{
    [connection cancel];
}

#pragma mark NSURLConnection delegate implementation

- (void)connection:(NSURLConnection*)conn 
                   didReceiveResponse:(NSURLResponse*)response 
{
    [[self client] URLProtocol:self 
            didReceiveResponse:response 
            cacheStoragePolicy:[request cachePolicy]];
    [receivedData setLength:0];
}


- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    [[self client] URLProtocol:self didLoadData:data];
    [receivedData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection*)conn
{
    [[self client] URLProtocolDidFinishLoading:self];
    [self setConnection:nil];
    if (requestTag != 0) {
        if (requestDelegate && 
            [requestDelegate respondsToSelector:
               @selector(finishedLoadingData:forURL:taggedWith:)]) {
            [requestDelegate finishedLoadingData:receivedData 
                                          forURL:[request URL] 
                                      taggedWith:requestTag];
        }
    }
}


- (void)connection:(NSURLConnection*)conn didFailWithError:(NSError*)error 
{
    [[self client] URLProtocol:self didFailWithError:error];
    [self setConnection:nil];
}
#定义请求头#标记@“x-mycustomurl-intercept”
+(BOOL)canInitWithRequest:(NSURLRequest*)请求
{
//检查请求上的自定义标头以断开
//由下面的[startLoading]创建的无限循环。
if([HttpHeaderField的请求值:请求\u头\u标记]){
返回否;
}
if([theRequest.URL.scheme caseInsensitiveCompare:@“http”]==SensorDeredName){
返回YES;
}
返回否;
}
+(NSURLRequest*)规范请求请求请求:(NSURLRequest*)请求
{
返回请求;
}
-(id)initWithRequest:(NSURLRequest*)请求
cachedResponse:(nsCachedUrResponse*)cachedResponse
客户:(id)客户
{
//在请求上添加自定义标头以断开
//由下面的[startLoading]创建的无限循环。
NSMutableURLRequest*newRequest=[theRequest mutableCopy];
[newRequest setValue:@”“用于HttpHeaderField:REQUEST_HEADER_TAG];
//现在继续处理这个“标记”的请求
self=[super initWithRequest:theRequest
cachedResponse:cachedResponse
客户:客户];
如果(自我){
//捕获接收到的数据
[自我设置请求:新请求];
receivedData=[[NSMutableData]retain];
}
[新请求释放];
回归自我;
}
-(无效)解除锁定
{
[连接释放];
[请求释放];
[接收数据发布];
[super dealoc];
}
-(空)惊人的
{
//像往常一样从网上加载数据,但将自己设置为代理
//所以我可以在收到响应数据时截取它。
[自设置连接:[NSURLConnection connectionWithRequest:请求委托:自];
}
-(无效)停止加载
{
[连接取消];
}
#pragma标记NSURLConnection委托实现
-(无效)连接:(NSURLConnection*)连接
didReceiveResponse:(NSURLRResponse*)响应
{
[[self-client]URL协议:self
接收方回复:回复
cacheStoragePolicy:[请求缓存策略]];
[接收数据集长度:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[[self-client]URLProtocol:self-didLoadData:data];
[接收数据附录数据:数据];
}
-(无效)连接完成加载:(NSURLConnection*)连接
{
[[self-client]URLProtocoldFinishLoading:self];
[自设置连接:无];
如果(requestTag!=0){
如果(请求代理&&
[请求代理响应选择器:
@选择器(finishedLoadingData:forURL:taggedWith:)]{
[requestDelegate完成加载数据:receivedData
forURL:[请求URL]
taggedWith:requestTag];
}
}
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
[[self-client]URLProtocol:self-didFailWithError:error];
[自设置连接:无];
}

FYI,我这里有一个改进的答案: