Vasily
Vasily

Reputation: 3790

How to modify User-Agent in WebView?

I can't find way to modify User-Agent string at WebView component. I need to display mobile version of site (place iPhone user-agent) on Mac in WebView. I've tried:

That:

NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent": "Custom-Agent"])

And that:

let userAgent = NSDictionary(object: "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/8.0 Mobile/11A465 Safari/9537.53", forKey: "UserAgent")
NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as! [String : AnyObject])

Nothing has worked. I've tried to place it in AppDelegate and right before WebView loadRequest method, no luck.

Upvotes: 0

Views: 1309

Answers (1)

Vasily
Vasily

Reputation: 3790

Thanks to xhruso00. I've tried a lot of methods, but answer is so simple! :)

Use WebView's method:

//  The receiver’s custom user-agent string.
//  The custom user-agent string is used for all URLs.
//  If nil, then the receiver constructs a user-agent 
//   string that produces the best rendering results for each URL.

web.customUserAgent = "Here is custom user-agent"

Upvotes: 2

Related Questions