Skip to content

WKWebView #2

Description

@aresxin
  • webview多次频繁请求,会回调到失败这里,出现NSURLErrorDomain Code=-999
    Fixed
    在处理Webview的加载失败的回调时,要注意拦截掉被取消的请求。
 func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        if error.code == NSURLErrorCancelled {
            return
        }
        
        showErrorPage()
    }
  • WKWebView 设置自定义UserAgent
    FIX
    通过applicationNameForUserAgent设置
    config的此属性与上一个属性不同,不是将设置的字符串完全变成你所设置的值,
    它将字符串集添加到WebView的默认UserAgent并执行它。
    这正好就是我们想要的,您所要做的就是设置要添加的字符串。
    修改后的UserAgent,如:Mozilla/5.0 (iPhone; CPU iPhone OS 13_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Custom UserAgent
let config = WKWebViewConfiguration()
config.applicationNameForUserAgent = "Custom User Agent"
let webview = WKWebView(frame: .zero, configuration: config)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions