iOS - 修改UserAgent (WKWebView & UIWebView)

今天说说 ‘UserAgent’ 这个特殊字符吧,因为我们后台有个判断是如果 ‘UserAgent’ 中如果包含字符 ‘iPad’ 那么就是1,否则是0。

在 ‘iPad’ 升级了 ‘iPadOS 13’ 以后 UserAgent 的内容被更改了, 之前字符串中的 'iPad' 更改为了 'Macintosh' , 所以很自然的客户有些功能不能使用了,下面我们简单说下这个问题吧,当然了写这篇博客的时候我相信,我的客户们已经在APP中自由的翱翔了~ 祝你们幸福鸭~ 

首先,介绍下 'UserAgent' 

User Agent (用户代理),简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。

所以你可以理解成一个 特殊字符,这个字符里有很多的属性来代表它的身份, iOS 这边具体有什么呢 你猜猜看?

 

 

第二,我把iPad 12 和 13的 UserAgent 截图了了解一下就好,从内容上看苹果真的要把 iPad 当 Mac 玩了。

升级前,系统是 iPad OS  12.4.1. 

升级后  ‘Macintosh’.

 

第三,你要知道 ‘UserAgent’ 是可以手动更改的,WkWebView 和 UIWebView 不一样下面这两种方法都写一下,另外提示一下 ‘UIWebView’ 不要再用了。

    WKWebView *WKWeb = [[WKWebView alloc] init];
    //一般的时候,我们可以通过JS 调用到userAgent,然后增加一些内容后赋值。
    [WKWeb evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
        NSString *oldUserAgent = result;
        NSString *newUserAgent = [NSString stringWithFormat:@"%@ %@",oldUserAgent,@"TheNewWordForUserAgent"];
        webView.myWKWebView.customUserAgent = newUserAgent;
    }];
    //二班的小伙伴,不需要以前的内容那么直接改。
    WKWeb.customUserAgent = @"这个属性可以直接调用,调用直接改比UIWebView 方便多了";
   //了解一下就可以
   UIWebView *UIWeb = [[UIWebView alloc] init];
   NSString *oldAgent = [UIWeb stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
   NSString *newAgent = [NSString stringWithFormat:@"%@ %@",oldAgent,@"TheNewWordForUserAgent"];
    
   NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
   [[NSUserDefaults standardUserDefaults] registerDefaults: dic];
   [[NSUserDefaults standardUserDefaults] synchronize];

好了,改与不改看需求,做与不做看自己。

感谢观看,学以致用更感谢!

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
请逐句解释一下下面的代码import java.util.Arrays; import org.apache.http.client.fluent.Request; import ece448.iot_sim.SimConfig; import ece448.iot_sim.Main; public class GradeP2 { public static void main(String[] args) throws Exception { SimConfig config = new SimConfig( 8080, Arrays.asList("xxxx", "yyyy", "zzzz.789"), null, null, null); try (Main m = new Main(config)) { Grading.run(new GradeP2(), 10); } } private String get(String pathParams) throws Exception { return Request.Get("http://127.0.0.1:8080"+pathParams) .userAgent("Mozilla/5.0") .connectTimeout(1000) .socketTimeout(1000) .execute().returnContent().asString(); } public boolean testCase00() throws Exception { String ret = get("/xxxx"); return (ret.indexOf("xxxx is off") != -1) && (ret.indexOf("xxxx is on") == -1) && (ret.indexOf("Power reading is 0.000") != -1); } public boolean testCase01() throws Exception { String ret = get("/xxxx?action=on"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase02() throws Exception { String ret = get("/xxxx?action=off"); return (ret.indexOf("xxxx is off") != -1) && (ret.indexOf("xxxx is on") == -1); } public boolean testCase03() throws Exception { String ret = get("/xxxx?action=on"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase04() throws Exception { String ret = get("/xxxx?action=toggle"); return (ret.indexOf("xxxx is off") != -1) && (ret.indexOf("xxxx is on") == -1); } public boolean testCase05() throws Exception { String ret = get("/xxxx?action=toggle"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase06() throws Exception { String ret = get("/yyyy"); return (ret.indexOf("yyyy is off") != -1) && (ret.indexOf("yyyy is on") == -1); } public boolean testCase07() throws Exception { String ret = get("/xxxx"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase08() throws Exception { String ret = get("/zzzz.789"); return (ret.indexOf("Power reading is 0.000") != -1); } public boolean testCase09() throws Exception { get("/zzzz.789?action=on"); Thread.sleep(1500); String ret = get("/zzzz.789"); return (ret.indexOf("Power reading is 789.000") != -1); } } private static final Logger logger = LoggerFactory.getLogger(HTTPCommands.class); }
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值