1 year ago

#382497

test-img

plato522

Changing User Agent in WKWebView on iPad

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?

ios

swift

wkwebview

wkwebviewconfiguration

0 Answers

Your Answer

Accepted video resources