本文主要是介绍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];
好了,改与不改看需求,做与不做看自己。
感谢观看,学以致用更感谢!
这篇关于iOS - 修改UserAgent (WKWebView UIWebView)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!