How to get the WKWebView 's userAgent immediately?

Using this method
  • (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ Nullable)(Nullable id, NSError * _Nullable error))completionHandler;

can't get the UserAgent immediately.

You cannot synchronously evaluate JavaScript, that is true.

If you are overriding the full user agent string via
Code Block
WKWebView.customUserAgent
then you don't need to fetch it, because you'll know what it is already.

If you change the user agent string on each navigation by modifying
Code Block
WKWebPagePreferences.preferredContentMode
then you'd need to re-fetch it after each navigation.

If you override part of the user agent via
Code Block
WKWebViewConfiguration.applicationNameForUserAgent
or you do not override it at all, you'd only need to fetch it once after creating the web view, as it wouldn't change after that.

WKWebView *webView = [WKWebView new];  NSString  *ua = [webView valueForKey:@"userAgent"];

How to get the WKWebView 's userAgent immediately?
 
 
Q