zoukankan      html  css  js  c++  java
  • iOS交互h5——user-agent

    User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串。

    在公司产品中,在userAgent中增加了XXXXX字段,用于标识客户端。

    我的需求是不光要能更改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。

  • 相关阅读:
    DockerFile体系结构
    Nignx(二) server_name 规则
    解决Redis分布式锁——死锁问题
    redis缓存穿透,缓存击穿,缓存雪崩原因+解决方案
    Docker_Linux
    正则例子
    部属流程
    Mysql insert语句的优化
    MySQL innodb_fast_shutdown参数讲解
    MySQL技术内幕InnoDB存储引擎(表&索引算法和锁)
  • 原文地址:https://www.cnblogs.com/sunjianfei/p/6557933.html
Copyright © 2011-2022 走看看