Мы можем получить userAgent
, используя приведенный ниже фрагмент кода,
import UIKit
import WebKit
class ViewController: UIViewController{
var WKwebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
getUserAgent()
}
func getUserAgent() {
let webConfiguration = WKWebViewConfiguration()
WKwebView = WKWebView(frame: .zero, configuration: webConfiguration)
WKwebView.evaluateJavaScript("navigator.userAgent", completionHandler: { (result, error) in
debugPrint(result as Any)
debugPrint(error as Any)
if let unwrappedUserAgent = result as? String {
print("userAgent: \(unwrappedUserAgent)")
} else {
print("failed to get the user agent")
}
})
}
}
Вывод : userAgent: Mozilla / 5.0 (iPhone; CPU iPhone OS 13_3) как Mac OS X) AppleWebKit / 605.1.15 (K HTML, как Gecko) Mobile / 15E148