更改 UIWebview 的useragent

获取UIWebView的useragent:

1
2
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];  
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

附加自定义字段到 Useragent中

1
2
3
4
NSString *newUagent = [NSString stringWithFormat:@"%@ appname/3.5.2",secretAgent];  
NSDictionary *dictionary = [[NSDictionary alloc]
initWithObjectsAndKeys:newUagent, @"UserAgent", nil nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

但应该注意的事,设置了useragent以后,需要过一段时间才能生效,所以一般是在appdelegate.m中就去改;如果在UIWebViewController初始化的时候再去更改的话,就有会导致有些请求还是原来的useragent

Contents
,