tcodeb tcodeb - 1 year ago 126
Swift Question

How to change User Agent?

Im making an app for Mac and the app is basically a

inside the
which can be accessed from the menu bar.

Its working fine, the webpages load from the menu bar. But the
loads the full page as if it was opened from
. How do I change this so that it opens the webpage as for example an iPhone?

Answer Source

As per the class reference:

WebView has a property named customUserAgent. Just set it to what you want.

var customUserAgent: String!

If you want to manipulate the application name inside that string use the applicationNameForUserAgent property:

var applicationNameForUserAgent: String!

To pretend coming from Mobile Safari on iOS 9.3,

yourWebViewInstance.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13E188a Safari/601.1"

should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download