From aedd2aa87d4fa2800bb3ed79fa2aeed9cbbd3568 Mon Sep 17 00:00:00 2001 From: Giorgio Mandolini Date: Wed, 4 Dec 2019 22:35:50 +0100 Subject: [PATCH] fix(ios): webview onlink is now called only on link activated (#11295) * fix(ios): webview onlink is now called only on link activated * feat(ios): webview beforeload event main frame indication * docs: update webview beforeload details * fix(ios): updated doc --- apidoc/Titanium/UI/WebView.yml | 6 ++++++ iphone/Classes/TiUIWebView.m | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/apidoc/Titanium/UI/WebView.yml b/apidoc/Titanium/UI/WebView.yml index 9dd237e4ad7..40e02ba6aca 100644 --- a/apidoc/Titanium/UI/WebView.yml +++ b/apidoc/Titanium/UI/WebView.yml @@ -452,6 +452,12 @@ events: constants: Titanium.UI.iOS.WEBVIEW_NAVIGATIONTYPE_* platforms: [ipad, iphone] + - name: isMainFrame + summary: Indicate if the event was generated from the main page or an iframe. + type: Boolean + platforms: [ipad, iphone] + since: "9.0.0" + - name: error summary: Fired when the web view cannot load the content. description: | diff --git a/iphone/Classes/TiUIWebView.m b/iphone/Classes/TiUIWebView.m index 5766c837eac..db175a609db 100644 --- a/iphone/Classes/TiUIWebView.m +++ b/iphone/Classes/TiUIWebView.m @@ -979,6 +979,18 @@ - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSSt [[TiApp app] showModalController:alertController animated:YES]; } +- (void)fireBeforeLoad:(nonnull WKNavigationAction *)navigationAction +{ + if ([[self proxy] _hasListeners:@"beforeload"]) { + [[self proxy] fireEvent:@"beforeload" + withObject:@{ + @"url" : navigationAction.request.URL.absoluteString, + @"navigationType" : @(navigationAction.navigationType), + @"isMainFrame" : NUMBOOL(navigationAction.targetFrame.isMainFrame), + }]; + } +} + - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler { if (_isViewDetached) { @@ -1008,20 +1020,13 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK } } - if ([[self proxy] _hasListeners:@"beforeload"]) { - [[self proxy] fireEvent:@"beforeload" - withObject:@{ - @"url" : navigationAction.request.URL.absoluteString, - @"navigationType" : @(navigationAction.navigationType) - }]; - } - // Use "onlink" callback property to decide the navigation policy KrollWrapper *onLink = [[self proxy] valueForKey:@"onlink"]; - if (onLink != nil) { + if (onLink != nil && navigationAction.navigationType == WKNavigationTypeLinkActivated) { JSValueRef functionResult = [onLink executeWithArguments:@[ @{ @"url" : navigationAction.request.URL.absoluteString } ]]; if (functionResult != NULL && JSValueIsBoolean([onLink.bridge.krollContext context], functionResult)) { if (JSValueToBoolean([onLink.bridge.krollContext context], functionResult)) { + [self fireBeforeLoad:navigationAction]; decisionHandler(WKNavigationActionPolicyAllow); } else { decisionHandler(WKNavigationActionPolicyCancel); @@ -1030,6 +1035,8 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WK } } + [self fireBeforeLoad:navigationAction]; + NSString *scheme = [navigationAction.request.URL.scheme lowercaseString]; if ([allowedURLSchemes containsObject:navigationAction.request.URL.scheme]) {