Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e71294a

Browse files
committed
[webview_flutter] add gesture navigation for iOS
This introduces a boolean for the allowsBackForwardNavigationGestures setting of the WKWebView in iOS. It has no effect on Android.
1 parent 9175e0f commit e71294a

File tree

9 files changed

+62
-3
lines changed

9 files changed

+62
-3
lines changed

packages/webview_flutter/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.3.18
22

33
* Add support for onPageStarted event.
4+
* Add setting for iOS to allow gesture based navigation.
45

56
## 0.3.17
67

packages/webview_flutter/example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class _WebViewExampleState extends State<WebViewExample> {
7474
onPageFinished: (String url) {
7575
print('Page finished loading: $url');
7676
},
77+
allowsBackForwardNavigationGestures: true,
7778
);
7879
}),
7980
floatingActionButton: favoriteButton(),

packages/webview_flutter/example/test_driver/webview_flutter_e2e.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66
import 'dart:convert';
7+
import 'dart:io';
78
import 'dart:typed_data';
89

910
import 'package:flutter/foundation.dart';
@@ -652,6 +653,36 @@ void main() {
652653
expect(currentUrl, 'https://www.google.com/');
653654
});
654655
});
656+
657+
testWidgets('launches with allowsBackForwardNavigationGestures on iOS',
658+
(WidgetTester tester) async {
659+
if (Platform.isIOS != true) {
660+
return;
661+
}
662+
663+
final Completer<WebViewController> controllerCompleter =
664+
Completer<WebViewController>();
665+
await tester.pumpWidget(
666+
Directionality(
667+
textDirection: TextDirection.ltr,
668+
child: SizedBox(
669+
width: 400,
670+
height: 300,
671+
child: WebView(
672+
key: GlobalKey(),
673+
initialUrl: 'https://flutter.dev/',
674+
allowsBackForwardNavigationGestures: true,
675+
onWebViewCreated: (WebViewController controller) {
676+
controllerCompleter.complete(controller);
677+
},
678+
),
679+
),
680+
),
681+
);
682+
final WebViewController controller = await controllerCompleter.future;
683+
final String currentUrl = await controller.currentUrl();
684+
expect(currentUrl, 'https://flutter.dev/');
685+
});
655686
}
656687

657688
// JavaScript booleans evaluate to different string values on Android and iOS.

packages/webview_flutter/ios/Classes/FlutterWebView.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ - (NSString*)applySettings:(NSDictionary<NSString*, id>*)settings {
260260
} else if ([key isEqualToString:@"userAgent"]) {
261261
NSString* userAgent = settings[key];
262262
[self updateUserAgent:[userAgent isEqual:[NSNull null]] ? nil : userAgent];
263+
} else if ([key isEqualToString:@"allowsBackForwardNavigationGestures"]) {
264+
NSNumber* allowsBackForwardNavigationGestures = settings[key];
265+
_webView.allowsBackForwardNavigationGestures =
266+
[allowsBackForwardNavigationGestures boolValue];
263267
} else {
264268
[unknownKeys addObject:key];
265269
}

packages/webview_flutter/lib/platform_interface.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class WebSettings {
232232
this.hasNavigationDelegate,
233233
this.debuggingEnabled,
234234
@required this.userAgent,
235+
this.allowsBackForwardNavigationGestures,
235236
}) : assert(userAgent != null);
236237

237238
/// The JavaScript execution mode to be used by the webview.
@@ -255,9 +256,14 @@ class WebSettings {
255256
/// See also [WebView.userAgent].
256257
final WebSetting<String> userAgent;
257258

259+
/// Whether to allow swipe based navigation in iOS.
260+
///
261+
/// See also: [WebView.allowsBackForwardNavigationGestures]
262+
final bool allowsBackForwardNavigationGestures;
263+
258264
@override
259265
String toString() {
260-
return 'WebSettings(javascriptMode: $javascriptMode, hasNavigationDelegate: $hasNavigationDelegate, debuggingEnabled: $debuggingEnabled, userAgent: $userAgent,)';
266+
return 'WebSettings(javascriptMode: $javascriptMode, hasNavigationDelegate: $hasNavigationDelegate, debuggingEnabled: $debuggingEnabled, userAgent: $userAgent, allowsBackForwardNavigationGestures: $allowsBackForwardNavigationGestures)';
261267
}
262268
}
263269

packages/webview_flutter/lib/src/webview_method_channel.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
137137
_addIfNonNull('jsMode', settings.javascriptMode?.index);
138138
_addIfNonNull('hasNavigationDelegate', settings.hasNavigationDelegate);
139139
_addIfNonNull('debuggingEnabled', settings.debuggingEnabled);
140+
_addIfNonNull('allowsBackForwardNavigationGestures',
141+
settings.allowsBackForwardNavigationGestures);
140142
_addSettingIfPresent('userAgent', settings.userAgent);
141143
return map;
142144
}

packages/webview_flutter/lib/webview_flutter.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class WebView extends StatefulWidget {
151151
this.userAgent,
152152
this.initialMediaPlaybackPolicy =
153153
AutoMediaPlaybackPolicy.require_user_action_for_all_media_types,
154+
this.allowsBackForwardNavigationGestures,
154155
}) : assert(javascriptMode != null),
155156
assert(initialMediaPlaybackPolicy != null),
156157
super(key: key);
@@ -311,6 +312,13 @@ class WebView extends StatefulWidget {
311312
/// The default policy is [AutoMediaPlaybackPolicy.require_user_action_for_all_media_types].
312313
final AutoMediaPlaybackPolicy initialMediaPlaybackPolicy;
313314

315+
/// A Boolean value indicating whether horizontal swipe gestures will trigger back-forward list navigations.
316+
///
317+
/// This only works on iOS.
318+
///
319+
/// By default `allowsBackForwardNavigationGestures` is false.
320+
final bool allowsBackForwardNavigationGestures;
321+
314322
@override
315323
State<StatefulWidget> createState() => _WebViewState();
316324
}
@@ -384,6 +392,8 @@ WebSettings _webSettingsFromWidget(WebView widget) {
384392
hasNavigationDelegate: widget.navigationDelegate != null,
385393
debuggingEnabled: widget.debuggingEnabled,
386394
userAgent: WebSetting<String>.of(widget.userAgent),
395+
allowsBackForwardNavigationGestures:
396+
widget.allowsBackForwardNavigationGestures,
387397
);
388398
}
389399

packages/webview_flutter/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: webview_flutter
22
description: A Flutter plugin that provides a WebView widget on Android and iOS.
3-
version: 0.3.17
3+
version: 0.3.18
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter
66

packages/webview_flutter/test/webview_flutter_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ void main() {
822822
await tester.pumpWidget(
823823
const WebView(
824824
initialUrl: 'https://youtube.com',
825+
allowsBackForwardNavigationGestures: true,
825826
),
826827
);
827828

@@ -837,6 +838,7 @@ void main() {
837838
hasNavigationDelegate: false,
838839
debuggingEnabled: false,
839840
userAgent: WebSetting<String>.of(null),
841+
allowsBackForwardNavigationGestures: true,
840842
),
841843
// TODO(iskakaushik): Remove this when collection literals makes it to stable.
842844
// ignore: prefer_collection_literals
@@ -1193,7 +1195,9 @@ class MatchesWebSettings extends Matcher {
11931195
_webSettings.hasNavigationDelegate ==
11941196
webSettings.hasNavigationDelegate &&
11951197
_webSettings.debuggingEnabled == webSettings.debuggingEnabled &&
1196-
_webSettings.userAgent == webSettings.userAgent;
1198+
_webSettings.userAgent == webSettings.userAgent &&
1199+
_webSettings.allowsBackForwardNavigationGestures ==
1200+
webSettings.allowsBackForwardNavigationGestures;
11971201
}
11981202
}
11991203

0 commit comments

Comments
 (0)