xcode 在代理(可可)后面使用 Web 视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3001804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using web view behind a proxy (cocoa)
提问by Cal S
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.
我正在创建一个 web 浏览器类型的应用程序(使用 web 视图对象),它需要能够通过代理连接到互联网。服务器、端口、用户名和密码都可以硬编码到应用程序中,但不幸的是,我不知道如何在不更改系统范围代理设置的情况下自定义 Web 视图的代理设置。
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)
如果您知道如何执行此操作,请提供一些示例代码,非常感谢!(另外,如果它有任何改变 - 我正在为 mac 开发,而不是 iPhone)
采纳答案by slf
The easiest way I know is to wire up a UIWebView delegateand listen to all requests before they go through, and redirect the ones you care about through ASIHttpRequestand your custom proxy settings.
我知道的最简单的方法是连接UIWebView 委托并在所有请求通过之前侦听它们,并通过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]
}
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.
这有点不稳定,但它应该可以工作。请记住正确管理您的内存并且不要使用这个泄漏的示例代码...... YMMV,我什至没有测试它是否编译,在浏览器窗口中输入所有内容并使用一些复制和粘贴技巧。