Changing User Agent in WKWebView on iPad

886 Views Asked by At

I noticed something strange with WKWebView user agents.

If I try setting it as

webViewConfiguration.defaultWebpagePreferences.preferredContentMode = .mobile
webViewConfiguration.applicationNameForUserAgent = "MYIOSAPP/\(appVersion)"

I get this:

Mozilla/5.0 (iPad; CPU OS 15_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) MYIOSAPP/1.0

However if I set the customUserAgent like so:

webView.customUserAgent = (webView.value(forKey: "userAgent") ?? "") + "MYIOSAPP/\(appVersion)"

I get this:

Mozilla/5.0 (iPad; CPU OS 15_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MYIOSAPP/1.0

What is causing it to lose the Mobile part of the user agent? Also why does it even need it if it already has the iOS version number in the string?

0

There are 0 best solutions below