2010-06-08 61 views
4

我正在創建一個需要能夠通過代理連接到互聯網的web瀏覽器類型的應用程序(使用web視圖對象)。服務器,端口,用戶名和密碼都可以硬編碼到應用程序中,但不幸的是,我不知道如何在不改變系統範圍代理設置的情況下自定義Web視圖的代理設置。在代理之後使用web視圖(可可)

如果你知道如何做到這一點,請提供一些示例代碼,非常感謝! (另外,如果改變任何東西 - 我開發的Mac,而不是iPhone)

回答

4

我所知道的最簡單的方法就是連接一個UIWebView delegate,聽所有請求他們通過之前,並重定向你所關心的那些大約通過ASIHttpRequest和您的自定義代理設置。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    // Configure a proxy server manually 
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ignore"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setProxyHost:@"192.168.0.1"]; 
    [request setProxyPort:3128]; 

    // Alternatively, you can use a manually-specified Proxy Auto Config file (PAC) 
    // (It's probably best if you use a local file) 
    [request setPACurl:[NSURL URLWithString:@"file:///Users/ben/Desktop/test.pac"]]; 

    // fire the request async 
    [request setDelegate:self]; 
    [request startAsynchronous]; 

    return NO; 
} 


- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    NSData *responseData = [request responseData]; 
    // todo: save data to disk and load with [self webView] 
} 

這有點過分,但它應該工作。只要記住要正確地管理你的內存,不要使用這個泄露的示例代碼... YMMV,我甚至沒有測試過這個編譯,在瀏覽器窗口中輸入了所有內容,並複製粘貼hackery。

相關問題