今天做webView网页的时候遇到一个问题就是设置http的头请求 在网上搜了一段代码
在你主要视图控制器或者程序主类(app delegate)里面添加以下函数:
+ (void)initialize {
}
但是使用的时候发现 http的头里面 就只有设置的这个字段了 ,其他的信息全部没有了 对后台去判断设备什么的都很不方便 没有办法 就有在网上瞎逛了一会但是没找到很好的解决办法 最后忽然大迷糊了一下 就明白了综合了一下网上的知识 写出了以下代码 测试了下可以使用 ^_^ 看的博客给出来谢谢这些大神们铺的路
http://www.mphweb.com/en/blog/easily-set-user-agent-uiwebview
http://blog.youkuaiyun.com/mangosnow/article/details/38798195
摸索了两个小时左右 希望给别人一些帮助 把自己的代码贴出来
[self setUserAgentForWoapp];
-(void)setUserAgentForWoapp
{
// Set user agent (the only problem is that we can't modify the User-Agent later in the program)
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *newUagent = [NSString stringWithFormat:@"%@/Your desired user agent",secretAgent];
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newUagent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
webView = nil;
}