Update 实际上我试图从其他来源找到它,但没有明确的答案将完美地工作。 (例如:How to request the desktop version of a webpage in Swift 2)
请帮帮忙。用可行的例子提供答案。
答案 0 :(得分:3)
WKWebView应该可以解决您的问题 - 您可以在代码中定义它。这样做:
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)
}
答案 1 :(得分:2)
试试这个:
import UIKit
class WebViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
override func viewDidAppear(animated: Bool) {
var url = NSURL(string: "https://www.samplepage.com")
var request = NSMutableURLRequest(URL: url)
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": "Custom-Agent"])
webView.loadRequest(request)
}
}
答案 2 :(得分:1)
Swift 3.0版本
let url = URL(string: "https://www.samplepage.com")
let request = URLRequest(url: url!)
UserDefaults.standard.register(defaults: ["UserAgent": "Custom-Agent"])
webView.loadRequest(request)
答案 3 :(得分:1)
这很棒! :)
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)