Set useragent in WKWebview

前端 未结 6 2145
野趣味
野趣味 2021-01-31 14:25

How do I set a custom useragent string in a WKWebView? I\'m trying to embed the version of my app so that my server-side can see what features are available. I found the followi

6条回答
  •  天命终不由人
    2021-01-31 15:04

    In my swift 3 case, I need entire app using a custom userAgent, here is my solution in AppDelegate. Here using UIWebview is because I don't need to set up the WKWebViewConfiguration, because I just only need the userAgent string

     fileprivate func setupGlobalWebviewUserAgent() {
    
        let webview = UIWebView()
        var newUserAgent = webview.stringByEvaluatingJavaScript(from: "navigator.userAgent")
        newUserAgent = newUserAgent?.appending("custom user agent")
        let dictionary = Dictionary(dictionaryLiteral: ("UserAgent", newUserAgent))
        UserDefaults.standard.register(defaults: dictionary)
    }
    

提交回复
热议问题