简体   繁体   中英

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?

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.

您可以使用

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM