gpt4 book ai didi

ios - 请求桌面站点 WKWebview 在 Swift 4 中不工作

转载 作者:行者123 更新时间:2023-11-28 05:55:30 24 4
gpt4 key购买 nike

我尝试了几乎所有的方法,包括 stackoverflow 和其他网站,以在 WKWebview 中请求桌面版网站,但没有一个解决方案适合我。

我试过的链接如下

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

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

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

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

我尝试在上面的主题上通过评论答案来获得答案,但没有得到回复所以我在这里提出我的问题,我希望你不会将其标记为重复

这是我尝试过的许多实现中的一个

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 {
}

我会要求,请在发布答案之前测试你的答案或建议,因为我已经尝试了很多其他的事情。

编辑-

上面的示例代码工作正常,问题是更改用户代理不会立即反射(reflect),重新安装时用户代理更改会反射(reflect)出来,所以我的问题是如何使用按钮切换请求桌面站点。

最佳答案

这是我测试过的代码:

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 {

}

结果页面: iPhone 7Plus screenshot of WKWebview with Quora.com site

干杯!

关于ios - 请求桌面站点 WKWebview 在 Swift 4 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51591964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com