2010-03-22 80 views
2

我有一個使用WebView託管Flash插件的桌面瀏覽器應用程序。 Flash插件會定期向外部網站請求新數據,然後將其作爲花哨的圖形繪製。攔截來自WebView Flash插件的Web請求

我想攔截這些Web請求並獲取數據(所以我可以通過Growl顯示它,而不是保留桌面窗口)。但最好的我可以告訴,由Flash提出的請求不會被普通的WebView代理拾取。

是否有另一個地方可以設置鉤子?我嘗試通過[NSURLCache setSharedURLCache]安裝一個自定義NSURLCache,但從來沒有調用。我也嘗試了一些其他類的方法(如NSCachedURLResponse),但找不到方法。任何想法?非常感謝!

回答

3

驚訝沒有人回答這個,其實很簡單。創建一個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]; 
} 
+0

僅供參考,我有一個改進的答案在這裏:http://stackoverflow.com/questions/3155359/webkit-how- DO-I-得到最含量的最資源 – starkos 2011-03-08 17:18:14