Cap-go / capacitor-inappbrowser

Capacitor plugin in app browser with urlChangeEvent
MIT License
66 stars 48 forks source link

Following deep links in WebViewDialog #98

Closed eljass closed 6 months ago

eljass commented 9 months ago

Currently if inside the webview links redirects to the deeplinked addresses the page does not load because URL is invalid (URL schema is not http:// or https://). So we are experiencing this sort of issues:

image (7)

Would it be possible to adjust WebView configs to open external application in these cases rather than trying to load the URL inside the WebView itself?

I assume the fix lies inside these lines: https://github.com/Cap-go/capacitor-inappbrowser/blob/f7962d27b2a7acc5ba9827d969f61abe7857af32/android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java#L314C1-L321C1

So instead of this:

@Override
public boolean shouldOverrideUrlLoading(
  WebView view,
  WebResourceRequest request
) {
  return false;
}

function could look something like this:

@Override
public boolean shouldOverrideUrlLoading(
  WebView view,
  WebResourceRequest request
) {
  Context context = view.getContext();
  String url = request.getUrl().toString();

  if (!url.startsWith("https://") && !url.startsWith("http://")) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(intent);
    return true;
  }

  return false;
}

Does this make sense?

riderx commented 6 months ago

It does did you tried your fix? if yes please open a PR and i merge it

eljass commented 6 months ago

Not yet, can do so soon though. For now we had another possible workaround for it with capacitor's app-launcher plugin. In this workaround we attach listener for urlChanged event in here and if the schema is something else than http or https we try to open another app with that URL by:

  1. Attach listener: addListener("urlChangeEvent", handleUrlChange)
  2. Inside handleUrlChange, check if URL can be opened: canOpenUrl(options: CanOpenURLOptions) => Promise
  3. And then open URL in another app if possible: openUrl(options: OpenURLOptions) => Promise

Although might be good to attach this deepLink handler directly into this plugin so I'll try that!

riderx commented 6 months ago

Thanks it's merged