Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.5.0

* Added `onUrlChanged` callback to platform callback handler.

## 1.4.0

* Added `loadFile` and `loadHtml` interface methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
case 'onPageStarted':
_platformCallbacksHandler.onPageStarted(call.arguments['url']!);
return null;
case 'onUrlChanged':
_platformCallbacksHandler.onUrlChanged(call.arguments['url']!);
return null;
case 'onWebResourceError':
_platformCallbacksHandler.onWebResourceError(
WebResourceError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ abstract class WebViewPlatformCallbacksHandler {
void onPageFinished(String url);

/// Invoked by [WebViewPlatformController] when a page is loading.
/// /// Only works when [WebSettings.hasProgressTracking] is set to `true`.
/// Only works when [WebSettings.hasProgressTracking] is set to `true`.
void onProgress(int progress);

/// Invoked by [WebViewPlatformController] when the webview's URL has changed.
///
/// Unlike [onPageStarted], [onProgress], and [onPageFinished],
/// [onUrlChanged] also fires when navigating without a full page load
/// e.g. when using a single page application.
void onUrlChanged(String url);

/// Report web resource loading error to the host application.
void onWebResourceError(WebResourceError error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.4.0
version: 1.5.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import 'package:webview_flutter_platform_interface/webview_flutter_platform_inte
void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('Tests on `plugin.flutter.io/webview_<channel_id>` channel', () {
group(
'Tests on `plugin.flutter.io/webview_<channel_id>` channel dart->native',
() {
const int channelId = 1;
const MethodChannel channel =
MethodChannel('plugins.flutter.io/webview_$channelId');
Expand Down Expand Up @@ -554,6 +556,40 @@ void main() {
});
});

group(
'Tests on `plugin.flutter.io/webview_<channel_id>` channel native->dart',
() {
const int channelId = 1;
final WebViewPlatformCallbacksHandler callbacksHandler =
MockWebViewPlatformCallbacksHandler();
final JavascriptChannelRegistry javascriptChannelRegistry =
MockJavascriptChannelRegistry();

MethodChannelWebViewPlatform(
channelId,
callbacksHandler,
javascriptChannelRegistry,
);

tearDown(() {
reset(callbacksHandler);
});

test('onUrlChanged', () async {
// Run
await ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage(
'plugins.flutter.io/webview_$channelId',
StandardMethodCodec()
.encodeMethodCall(MethodCall('onUrlChanged', {'url': 'Test Url'})),
null,
);

// Verify
verify(callbacksHandler.onUrlChanged('Test Url'));
});
});

group('Tests on `plugins.flutter.io/cookie_manager` channel', () {
const MethodChannel cookieChannel =
MethodChannel('plugins.flutter.io/cookie_manager');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.5.0

* Support new `onUrlChanged` event in platform interface.

## 2.4.0

* Implemented new `loadFile` and `loadHtmlString` methods from the platform interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,38 @@ - (void)testWebViewWebEvaluateJavaScriptShouldNotBeCalledWhenShouldEnableZoomIsT
[self.navigationDelegate webView:webview didFinishNavigation:navigation];
}

- (void)testWebViewObserveValueForKeyPathCallsMethodChannelOnURLChange {
[self.navigationDelegate
observeValueForKeyPath:@"URL"
ofObject:nil
change:@{
NSKeyValueChangeNewKey : [NSURL URLWithString:@"https://flutter.dev/"]
}
context:nil];
OCMVerify([self.mockMethodChannel
invokeMethod:@"onUrlChanged"
arguments:[OCMArg isEqual:@{@"url" : @"https://flutter.dev/"}]]);
}

- (void)testWebViewObserveValueForKeyPathReturnsForNonURLChanges {
[self.navigationDelegate
observeValueForKeyPath:@"IRRELEVANT_PATH"
ofObject:nil
change:@{
NSKeyValueChangeNewKey : [NSURL URLWithString:@"https://flutter.dev/"]
}
context:nil];

OCMReject([self.mockMethodChannel invokeMethod:[OCMArg any] arguments:[OCMArg any]]);
}

- (void)testWebViewObserveValueForKeyPathReturnsForNSNullURLChanges {
[self.navigationDelegate observeValueForKeyPath:@"URL"
ofObject:nil
change:@{NSKeyValueChangeNewKey : [NSNull null]}
context:nil];

OCMReject([self.mockMethodChannel invokeMethod:[OCMArg any] arguments:[OCMArg any]]);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ typedef void PageFinishedCallback(String url);
/// Signature for when a [WebView] is loading a page.
typedef void PageLoadingCallback(int progress);

/// Signature for when a [WebView] changed its current URL.
typedef void UrlChangedCallback(String url);

/// Signature for when a [WebView] has failed to load a resource.
typedef void WebResourceErrorCallback(WebResourceError error);

Expand All @@ -61,6 +64,7 @@ class WebView extends StatefulWidget {
this.onPageStarted,
this.onPageFinished,
this.onProgress,
this.onUrlChanged,
this.onWebResourceError,
this.debuggingEnabled = false,
this.gestureNavigationEnabled = false,
Expand Down Expand Up @@ -175,6 +179,9 @@ class WebView extends StatefulWidget {
/// Invoked when a page is loading.
final PageLoadingCallback? onProgress;

/// Invoked when a webview's URL has changed.
final UrlChangedCallback? onUrlChanged;

/// Invoked when a web resource has failed to load.
///
/// This callback is only called for the main page.
Expand Down Expand Up @@ -645,6 +652,13 @@ class _PlatformCallbacksHandler implements WebViewPlatformCallbacksHandler {
}
}

@override
void onUrlChanged(String url) {
if (_webView.onUrlChanged != null) {
_webView.onUrlChanged!(url);
}
}

void onWebResourceError(WebResourceError error) {
if (_webView.onWebResourceError != null) {
_webView.onWebResourceError!(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ - (instancetype)initWithChannel:(FlutterMethodChannel *)channel {
return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context {
if (![keyPath isEqualToString:@"URL"]) {
return;
}
NSURL *newURL = [change objectForKey:NSKeyValueChangeNewKey];
if (![newURL isEqual:[NSNull null]]) {
[_methodChannel invokeMethod:@"onUrlChanged" arguments:@{@"url" : newURL.absoluteString}];
}
}

#pragma mark - WKNavigationDelegate conformance

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ - (instancetype)initWithFrame:(CGRect)frame
_webView.scrollView.automaticallyAdjustsScrollIndicatorInsets = NO;
}
}
[_webView addObserver:_navigationDelegate
forKeyPath:@"URL"
options:NSKeyValueObservingOptionNew
context:NULL];

[self applySettings:settings];
// TODO(amirh): return an error if apply settings failed once it's possible to do so.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_wkwebview
description: A Flutter plugin that provides a WebView widget based on Apple's WKWebView control.
repository: https://github.com/flutter/plugins/tree/master/packages/webview_flutter/webview_flutter_wkwebview
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 2.4.0
version: 2.5.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -18,7 +18,9 @@ flutter:
dependencies:
flutter:
sdk: flutter
webview_flutter_platform_interface: ^1.3.0
#TODO (BeMacized): CHANGE TO VERSION DEPENDENCY ONCE UPDATED PLATFORM INTERFACE HAS BEEN PUBLISHED.
webview_flutter_platform_interface:
path: ../webview_flutter_platform_interface

dev_dependencies:
flutter_driver:
Expand Down