Post

使用wkwebview打开页面会被提示不是Safari的及自定义UserAgent问题

参考:

App内使用WKWebview,有些h5会根据UserAgent判断是在Safari里还是APP内,在APP内会提示进入Safari,体验不好,可以在在APP里设置特定的内容干扰判断提高体验

customUserAgent > UserDefault > applicationNameForUserAgent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class TestVC: UIViewController {
    var webview: WKWebView = {
        let config = WKWebViewConfiguration()
        // 方式一、设置applicationNameForUserAgent
        // config.applicationNameForUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1"
        let v = WKWebView(frame: .zero, configuration: config)
        if #available(iOS 16.4,*) {
            v.isInspectable = true
        } else {
            // Fallback on earlier versions
        }
        // 方式二、设置customUserAgent
        let oldUserAgent = v.value(forKey: "userAgent") as? String ?? ""
        v.customUserAgent = "\(oldUserAgent) Safari/604.1"
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(webView)
        if let url = URL(string: "https://wwww.google.com") {
            self.webView.load(URLRequest(url: url))
        }
    }
}
This post is licensed under CC BY 4.0 by the author.