flutterwebviewuser-agent

How to get the current user agent used by WebView before load an url?


I'm looking for a way to get the actual Flutter Webview User Agent before loading an url.
It seems not be possible...


Solution

  • Hey :) I'm a little late, but to answer your question, yes this is defintely possible and here's how I've done it in the past:

    After you've set your web view controller, e.g.:

    import 'package:webview_flutter/webview_flutter.dart';
    ....
    late WebViewController webViewControllerObj;
    ....
    webViewControllerObj = WebViewController.fromPlatformCreationParams(paramsObj);
    if (webViewControllerObj.platform is WebKitWebViewController) {
       (webViewControllerObj.platform as WebKitWebViewController).setInspectable(true);
    }
    webViewControllerObj
          ..setJavaScriptMode(JavaScriptMode.unrestricted)
          ..addJavaScriptChannel('Flutter', onMessageReceived: handleWebMessage)
          ..setNavigationDelegate(NavigationDelegate(
     ........
    

    You can retrieve the userAgent string before loading the url like so:

    var userAgentStr = await webViewControllerObj.getUserAgent();
    

    Hope this helps, let me know if you have any questions - Amy :)