Skip to content

Commit 6ae3013

Browse files
authored
Add getWebViewVersion() method (#197)
* Add method WebviewController.getWebViewVersion * Use Utf8FromUtf16 instead of CW2A * Update README.md
1 parent 662021b commit 6ae3013

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ A [Flutter](https://flutter.dev/) WebView plugin for Windows built on [Microsoft
77

88

99
### Target platform requirements
10-
- [WebView2 Runtime](https://developer.microsoft.com/en-us/microsoft-edge/webview2/)
10+
- [WebView2 Runtime](https://developer.microsoft.com/en-us/microsoft-edge/webview2/)
11+
Before initializing the webview, call `getWebViewVersion()` to check whether the required **WebView2 Runtime** is installed or not on the current system. If `getWebViewVersion()` returns null, guide your user to install **WebView2 Runtime** from this [page](https://developer.microsoft.com/en-us/microsoft-edge/webview2/).
1112
- Windows 10 1809+
1213

1314
### Development platform requirements

lib/src/webview.dart

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class WebviewController extends ValueNotifier<WebviewValue> {
8383
});
8484
}
8585

86+
/// Get the browser version info including channel name if it is not the
87+
/// WebView2 Runtime.
88+
/// Returns [null] if the webview2 runtime is not installed.
89+
static Future<String?> getWebViewVersion() async {
90+
return _pluginChannel.invokeMethod<String>('getWebViewVersion');
91+
}
92+
8693
late Completer<void> _creatingCompleter;
8794
int _textureId = 0;
8895
bool _isDisposed = false;

windows/webview_windows_plugin.cc

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "webview_bridge.h"
1313
#include "webview_host.h"
1414
#include "webview_platform.h"
15+
#include "util/string_converter.h"
1516

1617
#pragma comment(lib, "dxgi.lib")
1718
#pragma comment(lib, "d3d11.lib")
@@ -21,6 +22,7 @@ namespace {
2122
constexpr auto kMethodInitialize = "initialize";
2223
constexpr auto kMethodDispose = "dispose";
2324
constexpr auto kMethodInitializeEnvironment = "initializeEnvironment";
25+
constexpr auto kMethodGetWebViewVersion = "getWebViewVersion";
2426

2527
constexpr auto kErrorCodeInvalidId = "invalid_id";
2628
constexpr auto kErrorCodeEnvironmentCreationFailed =
@@ -140,6 +142,16 @@ void WebviewWindowsPlugin::HandleMethodCall(
140142
return result->Success();
141143
}
142144

145+
if (method_call.method_name().compare(kMethodGetWebViewVersion) == 0) {
146+
LPWSTR version_info = nullptr;
147+
auto hr = GetAvailableCoreWebView2BrowserVersionString(nullptr, &version_info);
148+
if (SUCCEEDED(hr) && version_info != nullptr) {
149+
return result->Success(flutter::EncodableValue(util::Utf8FromUtf16(version_info)));
150+
} else {
151+
return result->Success();
152+
}
153+
}
154+
143155
if (method_call.method_name().compare(kMethodInitialize) == 0) {
144156
return CreateWebviewInstance(std::move(result));
145157
}

0 commit comments

Comments
 (0)