RyanTCB RyanTCB - 4 months ago 35
Swift Question

Load desktop version WKWebView iOS 9

Up until recently

let url = NSURL (string:http://asite.com)
let request = NSMutableURLRequest(URL: url!)

//iOS loads the mobile version of asite.com which does not have the required DOM so we force the desktop version by setting new value forHTTPHeadrField
let newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.6 Safari/537.11"

request.setValue(newUserAgent, forHTTPHeaderField: "User_Agent")

let config = WKWebViewConfiguration()

//even though we dont need to see it the webpage needs to appear but we set its frame to CGRectZero so its hidden from user

let ghostWebView : WKWebView = WKWebView(frame:CGRectZero, configuration: config)

This would force the desktop version of the site. However it has just stopped working. Not sure exactly when but very recently.

Any ideas why?

Also google results show some use


for the HTTPHeaderField and others


whats the difference between the two?

I solved the issue by changing the User Agent string to

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"

Which I got from


So my new question is how often so these user agent strings change and is there a way for my app to auto update to the newest one?


To help any who find themselves here for an answer. The solution was

UserDefaults.standard.register(defaults: ["UserAgent" : "Chrome Safari"])