我正在尝试在我的应用上添加移动版,桌面版网站功能。但问题是User Agent
只在webView加载之前立即更改。在我的应用中,我添加了Mobile
的按钮1和Desktop
版本网站的第二个按钮。 UIWebView
在开始时选择User Agent
。任何人都可以告诉我有没有办法在运行时更改User Agent
。感谢
var defaultUserAgent = String()
@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.youtube.com")
let request = NSURLRequest(URL: url!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
@IBAction func mobile(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": defaultUserAgent])
print(defaultUserAgent)
let url = NSURL(string: "https://www.youtube.com")
let request = NSURLRequest(URL: url!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
@IBAction func desktop(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().registerDefaults(["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"])
let url = NSURL(string: "https://www.youtube.com")
let request = NSURLRequest(URL: url!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
print(webView.stringByEvaluatingJavaScriptFromString("navigator.userAgent"))
defaultUserAgent = webView.stringByEvaluatingJavaScriptFromString("navigator.userAgent")!
}
答案 0 :(得分:4)
我建议您使用 WkWebView 而不是 UIWebView ,因为它相对更强大
iOS 9的WkWebView具有直接API
$('#text_to speech').click(function(e){
$.ajax({ type: 'GET', data: { text: "text to speech" }, url: '/test' , success: function(response) { console.log('success'); } });
}