ios UIWebView iOS5 更改用户代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8487581/
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
UIWebView iOS5 changing user-agent
提问by 0xSina
How can I change the user-agent of UIWebView in iOS 5?
如何在 iOS 5 中更改 UIWebView 的用户代理?
What I have done so far:Using the delegate call back, intercept the NSURLRequest, create a new url request and set it's user-agent as whatever I want, then download the data and reload the UIWebView with "loadData:MIMEType:....".
到目前为止我所做的:使用委托回调,拦截 NSURLRequest,创建一个新的 url 请求并将它的用户代理设置为我想要的任何内容,然后下载数据并使用“loadData:MIMEType:...”重新加载 UIWebView。 .”。
Problem:This causes infinite recursion, where I load the data, which calls the delegate back, which intern calls the delegate....
问题:这会导致无限递归,在那里我加载数据,它调用委托,实习生调用委托......
Here's the delegate method:
这是委托方法:
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
dispatch_async(kBgQueue, ^{
NSURLResponse *response = nil;
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[request URL]];
NSDictionary *headers = [NSDictionary dictionaryWithObject:
@"custom_test_agent" forKey:@"User-Agent"];
[newRequest setAllHTTPHeaderFields:headers];
[self setCurrentReqest:newRequest];
NSData *data = [NSURLConnection sendSynchronousRequest:newRequest
returningResponse:&response
error:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
[webView loadData:data
MIMEType:[response MIMEType]
textEncodingName:[response textEncodingName]
baseURL:[request URL]];
});
});
return YES;
}
回答by Martin Wickman
Change the "UserAgent" default value by running this code once when your app starts:
通过在您的应用程序启动时运行一次此代码来更改“UserAgent”默认值:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Your user agent", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
EDIT: I have used this with great success, but want to add additional details. To get a user agent, you can enable the "Developer" menu, set the user agent, and then connect to this site to get it printed out for you: WhatsMyAgent. Likewise you can connect using any kind of mobile device, and get it that way too. BTW this is still working just fine in iOS7+
编辑:我已经成功地使用了它,但想添加其他细节。要获得用户代理,您可以启用“开发人员”菜单,设置用户代理,然后连接到此站点为您打印出来:WhatsMyAgent。同样,您可以使用任何类型的移动设备进行连接,也可以通过这种方式进行连接。顺便说一句,这在 iOS7+ 中仍然可以正常工作
回答by Zaid Pathan
In Swiftuse this to set UserAgent,
在Swift 中使用它来设置UserAgent,
func setUserAgent(){
var userAgent = NSDictionary(objectsAndKeys: "YourUserAgentName","UserAgent")
NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])
}
Use this to test,
用这个来测试,
println(WebView.stringByEvaluatingJavaScriptFromString("navigator.userAgent"));
回答by funberry
When you send message [aWebView loadData:MIMEType:textEncodingName:baseURL:]
当你发送消息 [aWebView loadData:MIMEType:textEncodingName:baseURL:]
then aWebView shouldStartLoadWithRequest:
will be called again, and then again - that is why you get an infinite recursion
thenaWebView shouldStartLoadWithRequest:
将被再次调用,然后再次调用 - 这就是为什么你会得到无限递归
You should restrict calling of your dispatch_async()
block, for example by using some conventional URL:
您应该限制对dispatch_async()
块的调用,例如通过使用一些传统的 URL:
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] isEqualToString:@"http://yourdomain.com/?local=true"]) {
return YES;
}
...
dispatch_async(...
[aWebView loadData:data
MIMEType:[response MIMEType]
textEncodingName:[response textEncodingName]
baseURL:[NSURL URLWithString:@"http://yourdomain.com/?local=true"]];
);
return NO;
}