请求桌面站点 WKWebview 在 Swift 4 中不起作用 [英] Request desktop site WKWebview not working in Swift 4

查看:43
本文介绍了请求桌面站点 WKWebview 在 Swift 4 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几乎所有一个 stackoverflow 和其他站点来请求 WKWebview 中站点的桌面版本,但没有一个解决方案对我有用.

我尝试过的链接如下

干杯!

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.

解决方案

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!

这篇关于请求桌面站点 WKWebview 在 Swift 4 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆