Skip to content

Commit f3090f0

Browse files
committed
Extended Web View API on iOS
flutter/flutter#136479 • Deprecates `WebViewController.loadFile(String)` in favor of `WebViewController.loadFileWithParams(LoadFileParams)`. • Introduces `AndroidLoadFileParams` and `WebKitLoadFileParams` to support platform-specific parameters when loading local HTML files on Android and iOS/macOS.
1 parent 4463066 commit f3090f0

22 files changed

+431
-70
lines changed

packages/webview_flutter/webview_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.14.0
2+
3+
* Deprecates `WebViewController.loadFile(String)` in favor of `WebViewController.loadFileWithParams(LoadFileParams)`.
4+
* Introduces `AndroidLoadFileParams` and `WebKitLoadFileParams` to support platform-specific parameters when loading local HTML files on Android and iOS/macOS.
5+
16
## 4.13.0
27

38
* Adds support to respond to recoverable SSL certificate errors. See `NavigationDelegate(onSSlAuthError)`.

packages/webview_flutter/webview_flutter/example/pubspec.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ dependencies:
2020
webview_flutter_android: ^4.7.0
2121
webview_flutter_wkwebview: ^3.22.0
2222

23+
dependency_overrides:
24+
webview_flutter_android:
25+
path: ../../webview_flutter_android
26+
webview_flutter_platform_interface:
27+
path: ../../webview_flutter_platform_interface
28+
webview_flutter_wkwebview:
29+
path: ../../webview_flutter_wkwebview
30+
2331
dev_dependencies:
2432
build_runner: ^2.1.5
2533
espresso: ^0.4.0

packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,25 @@ class WebViewController {
131131
/// `/Users/username/Documents/www/index.html`.
132132
///
133133
/// Throws a `PlatformException` if the [absoluteFilePath] does not exist.
134+
@Deprecated('Use loadFileWithParams(LocalFileParams params) instead. '
135+
'This method will be removed in a future release.')
134136
Future<void> loadFile(String absoluteFilePath) {
135137
return platform.loadFile(absoluteFilePath);
136138
}
137139

140+
/// Loads a local HTML file using the provided [params].
141+
///
142+
/// The [params] object should contain the absolute path to the
143+
/// file as it is stored on the device. For example:
144+
/// `/Users/username/Documents/www/index.html`.
145+
/// In addition, it may include platform-specific fields,
146+
/// such as headers (on Android) or resource paths (on iOS/macOS).
147+
///
148+
/// Throws a `PlatformException` if the [absoluteFilePath] does not exist.
149+
Future<void> loadFileWithParams(LoadFileParams params) {
150+
return platform.loadFileWithParams(params);
151+
}
152+
138153
/// Loads the Flutter asset specified in the pubspec.yaml file.
139154
///
140155
/// Throws a `PlatformException` if [key] is not part of the specified assets

packages/webview_flutter/webview_flutter/pubspec.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter
22
description: A Flutter plugin that provides a WebView widget backed by the system webview.
33
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 4.13.0
5+
version: 4.14.0
66

77
environment:
88
sdk: ^3.6.0
@@ -25,6 +25,14 @@ dependencies:
2525
webview_flutter_platform_interface: ^2.13.0
2626
webview_flutter_wkwebview: ^3.22.0
2727

28+
dependency_overrides:
29+
webview_flutter_android:
30+
path: ../webview_flutter_android
31+
webview_flutter_platform_interface:
32+
path: ../webview_flutter_platform_interface
33+
webview_flutter_wkwebview:
34+
path: ../webview_flutter_wkwebview
35+
2836
dev_dependencies:
2937
build_runner: ^2.1.5
3038
flutter_test:

packages/webview_flutter/webview_flutter/test/webview_controller_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ void main() {
2727
verify(mockPlatformWebViewController.loadFile('file/path'));
2828
});
2929

30+
test('loadFileWithParams', () async {
31+
final MockPlatformWebViewController mockPlatformWebViewController =
32+
MockPlatformWebViewController();
33+
34+
final WebViewController webViewController = WebViewController.fromPlatform(
35+
mockPlatformWebViewController,
36+
);
37+
38+
await webViewController.loadFileWithParams(
39+
const LoadFileParams(absoluteFilePath: 'file/path'),
40+
);
41+
verify(mockPlatformWebViewController.loadFileWithParams(
42+
const LoadFileParams(absoluteFilePath: 'file/path'),
43+
));
44+
});
45+
3046
test('loadFlutterAsset', () async {
3147
final MockPlatformWebViewController mockPlatformWebViewController =
3248
MockPlatformWebViewController();

packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ class MockPlatformWebViewController extends _i1.Mock
7878
returnValueForMissingStub: _i5.Future<void>.value(),
7979
) as _i5.Future<void>);
8080

81+
@override
82+
_i5.Future<void> loadFileWithParams(_i2.LoadFileParams? params) =>
83+
(super.noSuchMethod(
84+
Invocation.method(#loadFileWithParams, [params]),
85+
returnValue: _i5.Future<void>.value(),
86+
returnValueForMissingStub: _i5.Future<void>.value(),
87+
) as _i5.Future<void>);
88+
8189
@override
8290
_i5.Future<void> loadFlutterAsset(String? key) => (super.noSuchMethod(
8391
Invocation.method(#loadFlutterAsset, [key]),

packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ class MockPlatformWebViewController extends _i1.Mock
9191
returnValueForMissingStub: _i7.Future<void>.value(),
9292
) as _i7.Future<void>);
9393

94+
@override
95+
_i7.Future<void> loadFileWithParams(_i2.LoadFileParams? params) =>
96+
(super.noSuchMethod(
97+
Invocation.method(#loadFileWithParams, [params]),
98+
returnValue: _i7.Future<void>.value(),
99+
returnValueForMissingStub: _i7.Future<void>.value(),
100+
) as _i7.Future<void>);
101+
94102
@override
95103
_i7.Future<void> loadFlutterAsset(String? key) => (super.noSuchMethod(
96104
Invocation.method(#loadFlutterAsset, [key]),

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.8.0
2+
3+
* Introduces `AndroidLoadFileParams`, a platform-specific extension of `LoadFileParams` for Android that adds support for `headers`.
4+
15
## 4.7.0
26

37
* Adds support to respond to recoverable SSL certificate errors. See `AndroidNavigationDelegate.setOnSSlAuthError`.

packages/webview_flutter/webview_flutter_android/example/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ dependencies:
1919
path: ../
2020
webview_flutter_platform_interface: ^2.13.0
2121

22+
dependency_overrides:
23+
webview_flutter_platform_interface:
24+
path: ../../webview_flutter_platform_interface
25+
2226
dev_dependencies:
2327
espresso: ^0.4.0
2428
flutter_test:

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_controller.dart

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,48 @@ import 'android_webkit_constants.dart';
1818
import 'platform_views_service_proxy.dart';
1919
import 'weak_reference_utils.dart';
2020

21+
/// Object specifying parameters for loading a local file in a
22+
/// [AndroidWebViewController].
23+
@immutable
24+
class AndroidLoadFileParams extends LoadFileParams {
25+
/// Constructs a [AndroidLoadFileParams], the subclass of a [LoadFileParams].
26+
///
27+
/// Optionally, [headers] map may be included, which contains key-value pairs
28+
/// that will be passed as additional HTTP headers when loading the file.
29+
AndroidLoadFileParams({
30+
required String absoluteFilePath,
31+
this.headers = _defaultHeaders,
32+
}) : super(
33+
absoluteFilePath: absoluteFilePath.startsWith('file://')
34+
? absoluteFilePath
35+
: Uri.file(absoluteFilePath).toString(),
36+
);
37+
38+
/// Constructs a [AndroidLoadFileParams] using a [LoadFileParams].
39+
factory AndroidLoadFileParams.fromLoadFileParams(
40+
LoadFileParams params, {
41+
Map<String, String> headers = _defaultHeaders,
42+
}) {
43+
return AndroidLoadFileParams(
44+
absoluteFilePath: params.absoluteFilePath,
45+
headers: headers,
46+
);
47+
}
48+
49+
/// Default empty headers used when no custom headers are provided.
50+
///
51+
/// This constant ensures that the [headers] parameter is never `null`,
52+
/// simplifying internal logic and avoiding the need for null checks.
53+
static const Map<String, String> _defaultHeaders = <String, String>{};
54+
55+
/// Additional HTTP headers to be included when loading the local file.
56+
///
57+
/// On Android, WebView supports adding headers when loading local or remote
58+
/// content. This can be useful for scenarios like authentication,
59+
/// content-type overrides, or custom request context.
60+
final Map<String, String> headers;
61+
}
62+
2163
/// Object specifying creation parameters for creating a [AndroidWebViewController].
2264
///
2365
/// When adding additional fields make sure they can be null or have a default
@@ -383,15 +425,19 @@ class AndroidWebViewController extends PlatformWebViewController {
383425
_webView.pigeon_instanceManager.getIdentifier(_webView)!;
384426

385427
@override
386-
Future<void> loadFile(
387-
String absoluteFilePath,
388-
) {
389-
final String url = absoluteFilePath.startsWith('file://')
390-
? absoluteFilePath
391-
: Uri.file(absoluteFilePath).toString();
428+
Future<void> loadFileWithParams(LoadFileParams params) {
429+
switch (params) {
430+
case final AndroidLoadFileParams params:
431+
_webView.settings.setAllowFileAccess(true);
432+
return _webView.loadUrl(
433+
params.absoluteFilePath,
434+
params.headers,
435+
);
392436

393-
_webView.settings.setAllowFileAccess(true);
394-
return _webView.loadUrl(url, <String, String>{});
437+
default:
438+
return loadFileWithParams(
439+
AndroidLoadFileParams.fromLoadFileParams(params));
440+
}
395441
}
396442

397443
@override

packages/webview_flutter/webview_flutter_android/pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_android
22
description: A Flutter plugin that provides a WebView widget on Android.
33
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 4.7.0
5+
version: 4.8.0
66

77
environment:
88
sdk: ^3.6.0
@@ -23,6 +23,10 @@ dependencies:
2323
meta: ^1.10.0
2424
webview_flutter_platform_interface: ^2.13.0
2525

26+
dependency_overrides:
27+
webview_flutter_platform_interface:
28+
path: ../webview_flutter_platform_interface
29+
2630
dev_dependencies:
2731
build_runner: ^2.1.4
2832
flutter_test:

0 commit comments

Comments
 (0)