views:

443

answers:

1

Hi, I'm creating a web-browser type app (using a web view object) that needs to be able to connect to the internet via a proxy. Server, port, username and password can all be hardcoded into the app but unfortunately I have no idea how to customise the proxy settings of a web view without changing the system wide proxy settings.

If you know how to do this please provide some example code, thanks a lot! (Also, if it changes anything - I'm developing for mac, not iPhone)

+1  A: 

The easiest way I know is to wire up a UIWebView delegate and listen to all requests before they go through, and redirect the ones you care about through ASIHttpRequest and your custom proxy settings.

- (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]
}

It's a bit wonky,but it should work. Just remember to manage your memory properly and don't use this leaky example code... YMMV, I haven't even tested if this compiles, typed it all in the browser window with some copy and paste hackery.

slf