无法使用 WKWebView 加载桌面站点
Unable to load desktop site using WKWebView
我无法使用 WKWebView 加载像 whatsapp web 这样的网站。该网站加载但它不允许我使用它,因为它说需要 Safari 10+。当我将其更改为“Chrome Safari”时,它仍然警告我需要 Chrome 60+ 才能工作。我该如何解决这个问题?
这是我的代码:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
let defaultURLString = "https://web.whatsapp.com"
override func viewDidLoad() {
super.viewDidLoad()
if let savedURL = UserDefaults.standard.url(forKey: "webViewUrl") {
// Load webview with url here
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.applicationNameForUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 RuxitSynthetic/1.0 v9399630793 t38550 ath9b965f92 altpub cvcv=2"
let webView = WKWebView(
frame: CGRect.zero,
configuration: wkWebConfig)
view = webView
webView.load(URLRequest(url: savedURL))!
} else {
// Load default url here
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.applicationNameForUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17"
let webView = WKWebView(
frame: CGRect.zero,
configuration: wkWebConfig)
view = webView
if let defaultURL = URL(string: self.defaultURLString) {
webView.load(URLRequest(url: defaultURL))
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let currentUrl = webView.url {
UserDefaults.standard.set(currentUrl, forKey: "webViewUrl")
}
}
}
日志:
2021-03-29 20:03:51.919329+0530 Whatsapp Web for iPad[13152:2083381] WF: === Starting WebFilter logging for process Whatsapp Web for iPad
2021-03-29 20:03:51.919378+0530 Whatsapp Web for iPad[13152:2083381] WF: _userSettingsForUser mobile: {
filterBlacklist = (
);
filterWhitelist = (
);
restrictWeb = 1;
useContentFilter = 0;
useContentFilterOverrides = 0;
whitelistEnabled = 0;
}
2021-03-29 20:03:51.919419+0530 Whatsapp Web for iPad[13152:2083381] WF: _WebFilterIsActive returning: NO
2021-03-29 20:03:52.268277+0530 Whatsapp Web for iPad[13152:2083410] Metal API Validation Enabled
是的,我确实在我的 info.plist 文件中将“允许任意加载”设置为“是”。
提前致谢!
首先,applicationNameForUserAgent 可能不是您想要的,因为它附加到默认用户代理字符串的末尾。您可能正在寻找 WKWebView.customUserAgent,它将完全覆盖默认用户代理。
对于设置桌面模式,WKNavigationDelegate 中有一个委托方法,您可以使用它来指定桌面模式。在iOS中介绍 13. 如果您不使用 customUserAgent 手动设置用户代理,那么 WKWebView 将自动使用桌面用户代理而不是移动用户代理。
@available(iOS 13.0, *)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
preferences.preferredContentMode = .desktop
decisionHandler(.allow, preferences)
}
我无法使用 WKWebView 加载像 whatsapp web 这样的网站。该网站加载但它不允许我使用它,因为它说需要 Safari 10+。当我将其更改为“Chrome Safari”时,它仍然警告我需要 Chrome 60+ 才能工作。我该如何解决这个问题?
这是我的代码:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
let defaultURLString = "https://web.whatsapp.com"
override func viewDidLoad() {
super.viewDidLoad()
if let savedURL = UserDefaults.standard.url(forKey: "webViewUrl") {
// Load webview with url here
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.applicationNameForUserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 RuxitSynthetic/1.0 v9399630793 t38550 ath9b965f92 altpub cvcv=2"
let webView = WKWebView(
frame: CGRect.zero,
configuration: wkWebConfig)
view = webView
webView.load(URLRequest(url: savedURL))!
} else {
// Load default url here
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.applicationNameForUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17"
let webView = WKWebView(
frame: CGRect.zero,
configuration: wkWebConfig)
view = webView
if let defaultURL = URL(string: self.defaultURLString) {
webView.load(URLRequest(url: defaultURL))
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let currentUrl = webView.url {
UserDefaults.standard.set(currentUrl, forKey: "webViewUrl")
}
}
}
日志:
2021-03-29 20:03:51.919329+0530 Whatsapp Web for iPad[13152:2083381] WF: === Starting WebFilter logging for process Whatsapp Web for iPad
2021-03-29 20:03:51.919378+0530 Whatsapp Web for iPad[13152:2083381] WF: _userSettingsForUser mobile: {
filterBlacklist = (
);
filterWhitelist = (
);
restrictWeb = 1;
useContentFilter = 0;
useContentFilterOverrides = 0;
whitelistEnabled = 0;
}
2021-03-29 20:03:51.919419+0530 Whatsapp Web for iPad[13152:2083381] WF: _WebFilterIsActive returning: NO
2021-03-29 20:03:52.268277+0530 Whatsapp Web for iPad[13152:2083410] Metal API Validation Enabled
是的,我确实在我的 info.plist 文件中将“允许任意加载”设置为“是”。
提前致谢!
首先,applicationNameForUserAgent 可能不是您想要的,因为它附加到默认用户代理字符串的末尾。您可能正在寻找 WKWebView.customUserAgent,它将完全覆盖默认用户代理。
对于设置桌面模式,WKNavigationDelegate 中有一个委托方法,您可以使用它来指定桌面模式。在iOS中介绍 13. 如果您不使用 customUserAgent 手动设置用户代理,那么 WKWebView 将自动使用桌面用户代理而不是移动用户代理。
@available(iOS 13.0, *)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
preferences.preferredContentMode = .desktop
decisionHandler(.allow, preferences)
}