Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.1

* Added a `currentUrl` accessor for the WebView controller to look up what URL
is being displayed.

## 0.1.0+1

* Fix null crash when initialUrl is unset on iOS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void onMethodCall(MethodCall methodCall, Result result) {
case "goForward":
goForward(methodCall, result);
break;
case "currentUrl":
currentUrl(methodCall, result);
break;
default:
result.notImplemented();
}
Expand Down Expand Up @@ -86,6 +89,10 @@ private void goForward(MethodCall methodCall, Result result) {
result.success(null);
}

private void currentUrl(MethodCall methodCall, Result result) {
result.success(webView.getUrl());
}

@SuppressWarnings("unchecked")
private void updateSettings(MethodCall methodCall, Result result) {
applySettings((Map<String, Object>) methodCall.arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
17 changes: 17 additions & 0 deletions packages/webview_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,25 @@ class WebViewExample extends StatelessWidget {
_controller.complete(webViewController);
},
),
floatingActionButton: favoriteButton(),
);
}

favoriteButton() {
return Builder(builder: (BuildContext context) {
return FloatingActionButton(
onPressed: () async {
if (_controller.isCompleted) {
Comment thread
efortuna marked this conversation as resolved.
Outdated
var url = await (await _controller.future).currentUrl();
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("Favorited $url")),
);
}
},
child: Icon(Icons.favorite),
);
});
}
}

class SampleMenu extends StatelessWidget {
Expand Down
8 changes: 8 additions & 0 deletions packages/webview_flutter/ios/Classes/FlutterWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ @implementation FLTWebViewController {
WKWebView* _webView;
int64_t _viewId;
FlutterMethodChannel* _channel;
NSString* _currentUrl;
}

- (instancetype)initWithFrame:(CGRect)frame
Expand Down Expand Up @@ -74,6 +75,8 @@ - (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self onGoBack:call result:result];
} else if ([[call method] isEqualToString:@"goForward"]) {
[self onGoForward:call result:result];
} else if ([[call method] isEqualToString:@"currentUrl"]) {
[self onCurrentUrl:call result:result];
} else {
result(FlutterMethodNotImplemented);
}
Expand Down Expand Up @@ -115,6 +118,11 @@ - (void)onGoForward:(FlutterMethodCall*)call result:(FlutterResult)result {
result(nil);
}

- (void)onCurrentUrl:(FlutterMethodCall*)call result:(FlutterResult)result {
_currentUrl = [[_webView URL] absoluteString];
result(_currentUrl);
}

- (void)applySettings:(NSDictionary<NSString*, id>*)settings {
for (NSString* key in settings) {
if ([key isEqualToString:@"jsMode"]) {
Expand Down
8 changes: 8 additions & 0 deletions packages/webview_flutter/lib/webview_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ class WebViewController {
return _channel.invokeMethod('loadUrl', url);
}

/// Accessor to the current URL that the WebView is displaying.
///
/// If initialUrl was never specified, currentUrl returns `null`.
Comment thread
efortuna marked this conversation as resolved.
Outdated
Comment thread
efortuna marked this conversation as resolved.
Outdated
Comment thread
efortuna marked this conversation as resolved.
Outdated
Future<String> currentUrl() async {
String url = await _channel.invokeMethod('currentUrl');
return url;
}

/// Checks whether there's a back history item.
///
/// Note that this operation is asynchronous, and it is possible that the "canGoBack" state has
Expand Down
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
version: 0.1.0+1
version: 0.1.1
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter

Expand Down
59 changes: 46 additions & 13 deletions packages/webview_flutter/test/webview_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {
final FakePlatformWebView platformWebView =
fakePlatformViewsController.lastCreatedView;

expect(platformWebView.currentUrl, 'https://youtube.com');
expect(platformWebView.url, 'https://youtube.com');
});

testWidgets('JavaScript mode', (WidgetTester tester) async {
Expand Down Expand Up @@ -73,7 +73,7 @@ void main() {

controller.loadUrl('https://flutter.io');

expect(platformWebView.currentUrl, 'https://flutter.io');
expect(platformWebView.url, 'https://flutter.io');
});

testWidgets('Invald urls', (WidgetTester tester) async {
Expand All @@ -92,14 +92,14 @@ void main() {
fakePlatformViewsController.lastCreatedView;

expect(() => controller.loadUrl(null), throwsA(anything));
expect(platformWebView.currentUrl, isNull);
expect(platformWebView.url, isNull);

expect(() => controller.loadUrl(''), throwsA(anything));
expect(platformWebView.currentUrl, isNull);
expect(platformWebView.url, isNull);

// Missing schema.
expect(() => controller.loadUrl('flutter.io'), throwsA(anything));
expect(platformWebView.currentUrl, isNull);
expect(platformWebView.url, isNull);
});

testWidgets("Can't go back before loading a page",
Expand Down Expand Up @@ -227,15 +227,15 @@ void main() {
final FakePlatformWebView platformWebView =
fakePlatformViewsController.lastCreatedView;

expect(platformWebView.currentUrl, 'https://youtube.com');
expect(platformWebView.url, 'https://youtube.com');

controller.loadUrl('https://flutter.io');

expect(platformWebView.currentUrl, 'https://flutter.io');
expect(platformWebView.url, 'https://flutter.io');

controller.goBack();

expect(platformWebView.currentUrl, 'https://youtube.com');
expect(platformWebView.url, 'https://youtube.com');
});

testWidgets('Go forward', (WidgetTester tester) async {
Expand All @@ -254,19 +254,49 @@ void main() {
final FakePlatformWebView platformWebView =
fakePlatformViewsController.lastCreatedView;

expect(platformWebView.currentUrl, 'https://youtube.com');
expect(platformWebView.url, 'https://youtube.com');

controller.loadUrl('https://flutter.io');

expect(platformWebView.currentUrl, 'https://flutter.io');
expect(platformWebView.url, 'https://flutter.io');

controller.goBack();

expect(platformWebView.currentUrl, 'https://youtube.com');
expect(platformWebView.url, 'https://youtube.com');

controller.goForward();

expect(platformWebView.currentUrl, 'https://flutter.io');
expect(platformWebView.url, 'https://flutter.io');
});


testWidgets('Current URL', (WidgetTester tester) async {
WebViewController controller;
await tester.pumpWidget(
WebView(
onWebViewCreated: (WebViewController webViewController) {
controller = webViewController;
},
),
);

expect(controller, isNotNull);

// Test a WebView without an explicitly set first URL.
expect(await controller.currentUrl(), null);

final FakePlatformWebView platformWebView =
Comment thread
efortuna marked this conversation as resolved.
Outdated
fakePlatformViewsController.lastCreatedView;

controller.loadUrl('https://youtube.com');
expect(platformWebView.url, 'https://youtube.com');
expect(await controller.currentUrl(), 'https://youtube.com');

controller.loadUrl('https://flutter.io');
expect(await controller.currentUrl(), 'https://flutter.io');

controller.goBack();
expect(await controller.currentUrl(), 'https://youtube.com');
});
}

Expand All @@ -290,7 +320,7 @@ class FakePlatformWebView {
List<String> history = <String>[];
int currentPosition = -1;

String get currentUrl => history.isEmpty ? null : history[currentPosition];
String get url => history.isEmpty ? null : history[currentPosition];
Comment thread
efortuna marked this conversation as resolved.
Outdated
JavaScriptMode javaScriptMode;

Future<dynamic> onMethodCall(MethodCall call) {
Expand Down Expand Up @@ -321,6 +351,9 @@ class FakePlatformWebView {
currentPosition = min(history.length - 1, currentPosition + 1);
return Future<void>.sync(() {});
break;
case 'currentUrl':
return Future<String>.value(url);
break;
}
return Future<void>.sync(() {});
}
Expand Down