How can I request a desktop version of a webpage using UIWebView in Swift 3.0?

折月煮酒 提交于 2019-11-30 00:03:03

问题


Update Actually I tried to find it from other sources, but there is nowhere clear answer that will be working perfectly. (example : How to request the desktop version of a webpage in Swift 2)

Please, help up. Provide the answer with the workable example.


回答1:


A WKWebView should solve your problem - you can define it in your code. Do this:

var webView : WKWebView!
override func loadView() {
    super.loadView()

    let config = WKWebViewConfiguration()
    webView = WKWebView(frame: self.view.frame, configuration: config)
    self.webView!.uiDelegate = self
    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"
    //load URL here
    let url = NSURL(string: "https://stackoverflow.com/")!
    webView.load(URLRequest(url: url as URL))
    self.view.addSubview(webView)


}



回答2:


Swift 3.0 version

let url = URL(string: "https://www.samplepage.com")

let request = URLRequest(url: url!)

UserDefaults.standard.register(defaults: ["UserAgent": "Custom-Agent"])

webView.loadRequest(request)



回答3:


This works great! :)

let url = URL(string: "https://www.samplepage.com")
let request = URLRequest(url: url!)

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"

webView.load(request)


来源:https://stackoverflow.com/questions/38227332/how-can-i-request-a-desktop-version-of-a-webpage-using-uiwebview-in-swift-3-0

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