Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView user-agent (swift)

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

like image 824
Afatsum Avatar asked Oct 11 '19 08:10

Afatsum


2 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

like image 83
Sasho Avatar answered Oct 10 '22 08:10

Sasho


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

 }];

The result is userAgent

like image 27
Code Farmer Avatar answered Oct 10 '22 06:10

Code Farmer