Request desktop site WKWebview not working in Swift 4

喜欢而已 提交于 2019-12-24 08:33:12

问题


I tried almost everything one stackoverflow and other site to request desktop version of site in WKWebview, but none of solution is working for me.

my tried links as below

https://stackoverflow.com/a/38228810/3145189

https://stackoverflow.com/a/38228810/3145189

https://stackoverflow.com/a/49646773/3145189

https://stackoverflow.com/a/48155481/3145189

i tried on above threads to get answers by commenting on answers, but got no reply so here i go with my question, i hope you won't mark it duplicate

Here is my one of implementation out of many i tried

import UIKit
import WebKit

class ViewController: UIViewController {
    var webview: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let config = WKWebViewConfiguration()
        webview = WKWebView(frame: self.view.frame, configuration: config)
        self.webview.uiDelegate = self


        let url = URL(string: "https://quora.com/")!
        var request = URLRequest(url: url)
        let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
        request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
        webview.load(request)

        //webview.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
        self.view.addSubview(webview)

    }
}

extension ViewController: WKUIDelegate {
}

I would request, please test you answer or suggestion before posting answer, as i have tried so many other things.

Edit-

Above sample code is working fine, problem is changing user-agent don't reflect immediately, on reinstallation user-agent change does reflect, so my question is how to toggle request desktop site by using button.


回答1:


Here's my tested code:

class ViewController: UIViewController {

    var webview: WKWebView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        createWebView()
        if let url = URL(string: "https://quora.com") {
            load(url: url)
        }
    }

    private func createWebView() {
        let config = WKWebViewConfiguration()
        let webview = WKWebView(frame: self.view.frame, configuration: config)
        webview.uiDelegate = self
        webview.navigationDelegate = self
        self.webview = webview
        self.view.addSubview(webview)
    }

    private func load(url: URL) {
        var request = URLRequest(url: url)
        let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36"
        request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
        webview?.load(request)
    }
}

extension ViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("\(#function)")
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print("\(#function): \(error)")
    }
}

extension ViewController: WKUIDelegate {

}

And the resulting page:

Cheers!



来源:https://stackoverflow.com/questions/51591964/request-desktop-site-wkwebview-not-working-in-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!