IOS 之为webView设置UserAgent

今天做webView网页的时候遇到一个问题就是设置http的头请求  在网上搜了一段代码 


在你主要视图控制器或者程序主类(app delegate)里面添加以下函数:

+ (void)initialize {

    // Set user agent (the only problem is that we can't modify the User-Agent later in the program)

    NSDictionary *dictionnary = [[NSDictionary alloc]initWithObjectsAndKeys:@"Your desired user agent"@"UserAgent"nil];

    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

    [dictionnary release];

}

但是使用的时候发现 http的头里面 就只有设置的这个字段了 ,其他的信息全部没有了  对后台去判断设备什么的都很不方便 没有办法 就有在网上瞎逛了一会但是没找到很好的解决办法  最后忽然大迷糊了一下 就明白了综合了一下网上的知识  写出了以下代码  测试了下可以使用  ^_^  看的博客给出来谢谢这些大神们铺的路  


http://www.mphweb.com/en/blog/easily-set-user-agent-uiwebview


http://blog.csdn.net/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;

}





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值