Skip to content

Commit

Permalink
fix(ios): webview onlink is now called only on link activated (#11295)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
slash-84 authored and lokeshchdhry committed Dec 4, 2019
1 parent 2c8e2ac commit aedd2aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
6 changes: 6 additions & 0 deletions apidoc/Titanium/UI/WebView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
25 changes: 16 additions & 9 deletions iphone/Classes/TiUIWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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]) {
Expand Down

0 comments on commit aedd2aa

Please sign in to comment.