Skip to content

Commit

Permalink
[webview_flutter_wkwebview] Fixes an exception caused by the `onUrlCh…
Browse files Browse the repository at this point in the history
…ange` callback returning a null url (flutter#3848)

While investigating another bug, I realized that loading the initial request with an invalid url, the callback returns a null NSURL.
  • Loading branch information
bparrishMines authored May 2, 2023
1 parent 96fd7c8 commit 6e9258f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.4.2

* Fixes an exception caused by the `onUrlChange` callback passing a null `NSUrl`.

## 3.4.1

* Fixes internal type conversion error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ class WebKitWebViewController extends PlatformWebViewController {
final UrlChangeCallback? urlChangeCallback =
controller._currentNavigationDelegate?._onUrlChange;
if (urlChangeCallback != null) {
final NSUrl url = change[NSKeyValueChangeKey.newValue]! as NSUrl;
urlChangeCallback(UrlChange(url: await url.getAbsoluteString()));
final NSUrl? url = change[NSKeyValueChangeKey.newValue] as NSUrl?;
urlChangeCallback(UrlChange(url: await url?.getAbsoluteString()));
}
break;
}
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/packages/tree/main/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: 3.4.1
version: 3.4.2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,56 @@ void main() {
expect(urlChange.url, 'https://www.google.com');
});

test('setPlatformNavigationDelegate onUrlChange to null NSUrl', () async {
final MockWKWebView mockWebView = MockWKWebView();

late final void Function(
String keyPath,
NSObject object,
Map<NSKeyValueChangeKey, Object?> change,
) webViewObserveValue;

final WebKitWebViewController controller = createControllerWithMocks(
createMockWebView: (
_, {
void Function(
String keyPath,
NSObject object,
Map<NSKeyValueChangeKey, Object?> change,
)? observeValue,
}) {
webViewObserveValue = observeValue!;
return mockWebView;
},
);

final WebKitNavigationDelegate navigationDelegate =
WebKitNavigationDelegate(
const WebKitNavigationDelegateCreationParams(
webKitProxy: WebKitProxy(
createNavigationDelegate: CapturingNavigationDelegate.new,
createUIDelegate: WKUIDelegate.detached,
),
),
);

final Completer<UrlChange> urlChangeCompleter = Completer<UrlChange>();
navigationDelegate.setOnUrlChange(
(UrlChange change) => urlChangeCompleter.complete(change),
);

await controller.setPlatformNavigationDelegate(navigationDelegate);

webViewObserveValue(
'URL',
mockWebView,
<NSKeyValueChangeKey, Object?>{NSKeyValueChangeKey.newValue: null},
);

final UrlChange urlChange = await urlChangeCompleter.future;
expect(urlChange.url, isNull);
});

test('webViewIdentifier', () {
final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},
Expand Down

0 comments on commit 6e9258f

Please sign in to comment.