iOS 修改UserAgent
WKWebView
//获取UserAgent
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero];
[wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
DLog(@"userAgent :%@", result);
}];
//修改UserAgent
NSString *newUserAgent = [userAgent stringByAppendingString:@" 12345678"];
self.webView = [[WKWebView alloc] init];
self.webView.navigationDelegate = self;
[self.webView setCustomUserAgent:newUserAgent];
UIWebView
//获取UserAgent
UIWebView * v = [[UIWebView alloc] init];
[v stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
//修改UserAgent,一定要写在application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)方法中才会生效
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":@"CustomUserAgent-UIWebView"}];
[[NSUserDefaults standardUserDefaults] synchronize];
通过比较两种设置方案,还是用WKWebView更加灵活稳定。毕竟从iOS 8开始就已经作为替代UIWebView的方案,iOS13这都快出了。另外更新的是SFSafariViewController,这玩意完全是调用了Safari浏览器,功能更强大。但是引用苹果官方文档的说法,如果想自定义的话需要用WKWebView:
-
If your app lets users view websites from anywhere on the Internet, use the
SFSafariViewController class. If your app customizes, interacts with, or controls the display of web content, use the WKWebView class. -
When you adopt SFSafariViewController and a user presses a link to peek at and then pop to the link’s destination, the user views web content from within your app. Tapping Done, the user returns to the view controller that was displayed before the web content was loaded. When you instead use the WKWebView class, Peek and Pop sends the user to Safari by default.
推荐阅读
-
iOS 14.3/iPadOS 14.3开发者预览版 Beta 2(18C5054c)怎么升级?
-
使用阿里云服务器的总结一----修改配置,阿里----
-
ASP.NET jQuery 实例10 动态修改hyperlink的URL值_jquery
-
PHP修改session_id示例代码_PHP
-
layuiAdmin修改时间范围选择bug
-
iOS15.4支持戴口罩解锁了!网友实测:支付也能用、无需Apple Watch
-
linux下passwd命令设置修改用户密码的方法
-
iOS 15.4支持戴口罩解锁 实测仅支持iPhone 12/13!速度超快 支付也能用
-
微信KaKaoTalk提示音如何设置? 微信KaKao提示铃声修改教程
-
iOS应用突然无法从App Store获取到内购(IAP)产品列表的解决