首页 > 其他 > 详细

userAgent

时间:2015-07-25 16:33:13      阅读:352      评论:0      收藏:0      [点我收藏+]

用户代理 User Agent,是指浏览器,它的信息包括硬件平台、系统软件、应用软件和用户个人偏好。在X.400电子系统中,用户代理是一种对数据打包、创造分组头,以及编址、传递消息的部件。用户代理并不是仅指浏览器,还包括搜索引擎。

 

 

IOS嵌入UIWebView后,网页里获取到的user-agent和Safari是一样的,为了可以区别是app里面的访问,我们可以修改user-agent来做到这一点.

我的需求是不光要能更改user-agent,而且要保留WebView 原来的user-agent 信息,也就是说我需要在其上追加信息。在stackOverflow上搜集了多方答案,最终汇总的解决方案如下:

在启动时,比如在AppDelegate 中添加如下代码

 

    //get the original user-agent of webview
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSLog(@"old agent :%@", oldAgent);
    
    //add my info to the new agent
    NSString *newAgent = [oldAgent stringByAppendingString:@" Jiecao/2.4.7 ch_appstore"];
    NSLog(@"new agent :%@", newAgent);
    
    //regist the new agent
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

 

这样,WebView在请求时的user-agent 就是我们设置的这个了,如果需要在WebView 使用过程中再次变更user-agent,则需要再通过这种方式修改user-agent, 然后再重新实例化一个WebView。

 

userAgent

原文:http://www.cnblogs.com/yintingting/p/4675933.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!