Skip to content

feat: Allow plugins to override navigation #2872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 6, 2020
Merged
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
13 changes: 13 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ private void loadWebView() {
}

public boolean launchIntent(Uri url) {
/*
* Give plugins the chance to handle the url
*/
for (Map.Entry<String, PluginHandle> entry : plugins.entrySet()) {
Plugin plugin = entry.getValue().getInstance();
if (plugin != null) {
Boolean shouldOverrideLoad = plugin.shouldOverrideLoad(url);
if (shouldOverrideLoad != null) {
return shouldOverrideLoad;
}
}
}

if (!url.toString().contains(appUrl) && !appAllowNavigationMask.matches(url.getHost())) {
try {
Intent openIntent = new Intent(Intent.ACTION_VIEW, url);
Expand Down
10 changes: 10 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -548,6 +549,15 @@ protected void handleOnStop() {}
*/
protected void handleOnDestroy() {}

/**
* Give the plugins a chance to take control when a URL is about to be loaded in the WebView.
* Returning true causes the WebView to abort loading the URL.
* Returning false causes the WebView to continue loading the URL.
* Returning null will defer to the default Capacitor policy
*/
@SuppressWarnings("unused")
public Boolean shouldOverrideLoad(Uri url) { return null; }

/**
* Start a new Activity.
*
Expand Down
25 changes: 23 additions & 2 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,29 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
NotificationCenter.default.post(name: Notification.Name(CAPNotifications.DecidePolicyForNavigationAction.name()), object: navigationAction)
let navUrl = navigationAction.request.url!

/*
* Give plugins the chance to handle the url
*/
if let plugins = bridge?.plugins {
for pluginObject in plugins {
let plugin = pluginObject.value
let selector = NSSelectorFromString("shouldOverrideLoad:")
if plugin.responds(to: selector) {
let shouldOverrideLoad = plugin.shouldOverrideLoad(navigationAction)
if (shouldOverrideLoad != nil) {
if (shouldOverrideLoad == true) {
decisionHandler(.cancel)
return
} else if shouldOverrideLoad == false {
decisionHandler(.allow)
return
}
}
}
}
}

if let allowNavigation = allowNavigationConfig, let requestHost = navUrl.host {
for pattern in allowNavigation {
if matchHost(host: requestHost, pattern: pattern.lowercased()) {
Expand All @@ -267,8 +290,6 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
return
}

// TODO: Allow plugins to handle this. See
// https://github.com/ionic-team/cordova-plugin-ionic-webview/blob/608d64191405b233c01a939f5755f8b1fdd97f8c/src/ios/CDVWKWebViewEngine.m#L609
decisionHandler(.allow)
}

Expand Down
7 changes: 7 additions & 0 deletions ios/Capacitor/Capacitor/CAPPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
- (void)addListener:(CAPPluginCall *)call;
- (void)removeListener:(CAPPluginCall *)call;
- (void)removeAllListeners:(CAPPluginCall *)call;
/**
* Give the plugins a chance to take control when a URL is about to be loaded in the WebView.
* Returning true causes the WebView to abort loading the URL.
* Returning false causes the WebView to continue loading the URL.
* Returning nil will defer to the default Capacitor policy
*/
- (NSNumber *)shouldOverrideLoad:(WKNavigationAction *)navigationAction;

// Called after init if the plugin wants to do
// some loading so the plugin author doesn't
Expand Down