capacitor-inappbrowser icon indicating copy to clipboard operation
capacitor-inappbrowser copied to clipboard

Capacitor Share Api

Open qliqdev opened this issue 1 year ago • 1 comments

Hello can not use Capasitor Share plugin while InAppBrowser's view controller is presented

await InAppBrowser.openWebView({
        url: '<some url>',
        title: 'Title',
        visibleTitle: false,
        toolbarType: ToolBarType.BLANK,
        isAnimated: false
});

await InAppBrowser.addListener('urlChangeEvent', async (event: UrlEvent) => {
        console.log(event.url);
        if (event.url.includes('#share')) {
          await Share.share({url: event.url});
        } else if (event.url.includes('#exit')) {
          await InAppBrowser.close();
        }
});

catching error in

https://github.com/ionic-team/capacitor-plugins/blob/c4f916521b1d14bfb2ddac651b1fdc33e4c29884/share/ios/Sources/SharePlugin/SharePlugin.swift#L67-L70

Are there any best practices how to user Capacitor Share plugin with capacitor-inappbrowser

qliqdev avatar Jan 26 '24 07:01 qliqdev

I have no idea why this catch in share plugin, but since it catch there where there is no apparent reason maybe try to open issue in SharePlugin repo?

riderx avatar Mar 02 '24 00:03 riderx

Yes, I am getting the same error, too. @qliqdev

I had to build the share button on my website that is being opened by this plugin.

gasci avatar May 28 '24 13:05 gasci

Again this is not a bug, you cannot show 2 native views on top of each others in capacitor. Close the browser first and this will work

riderx avatar May 29 '24 07:05 riderx

Unfortunately, the close method doesn't close the webview, either. It just puts it to the background. Therefore, we are not able to use the Share API and this plugin together. Please test them together and you will also experience the same issue. (I mean when we open a webview)

gasci avatar May 29 '24 12:05 gasci

This make more sense, do you have the issue on android or iOS ?

riderx avatar May 29 '24 14:05 riderx

Oh sorry that I haven't stated it beforehand. It is on iOS 17.4.1. Capacitor 6.0.0.

gasci avatar May 29 '24 14:05 gasci

SharePlugin prevents from opening another ViewController because of this code

if self?.bridge?.viewController?.presentedViewController != nil {
    call.reject("Can't share while sharing is in progress")
    return
}

https://github.com/ionic-team/capacitor-plugins/blob/c4f916521b1d14bfb2ddac651b1fdc33e4c29884/share/ios/Sources/SharePlugin/SharePlugin.swift#L67-L70

for now i'm using Share.open() after await InAppBrowser.close();. This is the only way i think

complete code looks like

await InAppBrowser.addListener('urlChangeEvent', async (event: UrlEvent) => {
  const url = new URL(event.url);
  if (url.hash?.startsWith('#share')) {
    await InAppBrowser.close();
    Share.open({...})
  } else if (url.hash?.startsWith('#exit')) {
    await InAppBrowser.close();
    await this.router.navigate(['/home']);
  }
});

qliqdev avatar May 31 '24 20:05 qliqdev