Afatsum
Afatsum

Reputation: 137

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

Upvotes: 11

Views: 14562

Answers (3)

Sasho
Sasho

Reputation: 3652

Fast, synchronous method with no closures, Swift 5, iOS 15 and iOS 16 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

This has passed the appstore review.

Upvotes: 22

Code Farmer
Code Farmer

Reputation: 194

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

 }];

The result is userAgent

Upvotes: 8

adamfootdev
adamfootdev

Reputation: 1195

This gets the user agent:

let userAgent = webView.customUserAgent!

Upvotes: -9

Related Questions