我正在寻找一种在加载 url 之前获取实际 Flutter Webview 用户代理的方法。
好像不太可能...
WebView(
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController controller) {
controller
.runJavascriptReturningResult('navigator.userAgent')
.then((String currentUserAgent) {
controller.loadUrl('https://stackoverflow.com/q/70882313/17473716');
}
}
)
我不是 Flutter 专家。
嘿:)我有点晚了,但是要回答你的问题,是的,这绝对是可能的,这就是我过去的做法:
设置 Web 视图控制器后,例如:
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(
........
您可以在加载 url 之前检索 userAgent 字符串,如下所示:
var userAgentStr = await webViewControllerObj.getUserAgent();
希望这有帮助,如果您有任何疑问,请告诉我 - 艾米:)