WKWebView user-agent (swift)

Viewed 8628

I'm trying to get the user-agent from WKWebView by using swift,

let webView = WKWebView(frame: .zero) useragentlabel.text = webView.configuration.applicationNameForUserAgent

but it return Mobile/15E148, it s can be that the user-agent ?

thanks

3 Answers

Fast, synchronous method with no closures, Swift 5, iOS 13 and iOS 14 tested:

let UA = WKWebView().value(forKey: "userAgent")

would have UA have (optional value) of:

Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148

for iOS 13.5 in iPhone XS Simulator

[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {

 }];

The result is userAgent

This gets the user agent:

let userAgent = webView.customUserAgent!
Related