tcodeb tcodeb - 3 months ago 16
Swift Question

How to change User Agent?

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

WebView
inside the
viewController
which can be accessed from the menu bar.

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

HAS HAS
Answer

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.

Comments