UIWebView 和 WKWebView 修改自定义UserAgent

分享 · solar · 发布于 9 个月前 · 1318 次阅读

UIWebView

在AppDelegate的applicationDidFinishLaunching:方法中增加如下代码,修改默认User Agent

 func applicationDidFinishLaunching(_ application: UIApplication) {

        let webView = UIWebView(frame: CGRect.zero)
        let userAgent = webView.stringByEvaluatingJavaScript(from: "navigator.userAgent")!
        ServiceApi.userAgent = userAgent
        UserDefaults.standard.register(defaults: ["UserAgent": userAgent + " XXXAPPClient/1.0"])
        UserDefaults.standard.synchronize()

    }

WKWebView

直接修改WKWebView customUserAgent 属性


        self.webView.customUserAgent = "....  XXXXClient/1.0"
共收到 0 条回复
回帖