react-native-share / react-native-share

Social share, sending simple data to other apps.
https://react-native-share.github.io/react-native-share
MIT License
3.64k stars 947 forks source link

Share.opennot returning promise in android #1405

Closed ShahilMangroliya closed 1 year ago

ShahilMangroliya commented 1 year ago

Steps to reproduce

  1. const shareRes = await Share.open({ url:url, message: text, });
  2. It's not resolving promise on android however it's in ios and older versions of react-native-share like 8.2.2

Expected behaviour

It should go to next line after sharing complete

Actual behaviour

It just stuck at await

Environment

react-native-share

Version: 9.0.2

robinsoncol commented 1 year ago

I'm also running into this issue on iOS

jaaywags commented 1 year ago

Seeing this issue on Android, version 9.2.1.

robinsoncol commented 1 year ago

@jaaywags What type of file are you trying to share and where are you trying to save the file?

jaaywags commented 1 year ago

@robinsoncol I was able to reproduce the issue with the following code. I call this function to initiate the share. The issue happens if I share the file with other apps (E.g. Gmail) or not (by click outside of the action sheet).

This only happens on Android. iOS is fine.

import Share from 'react-native-share';
import RNFS from 'react-native-fs';
import { Buffer } from 'buffer';

// ...

// I am using exporting to control spinner and disable button
setExporting(true);
try {
  const rows = 'column1,column2,column3\nr1c1,r1c2,r1c3\n,r2c1,r2c2,r2c3\nr3c1,r3c2,r3c3';

  const filename = `${dayjs().format('MMDDYYYYTHHmmss')}`;
  let path = '';
  if (Platform.OS === 'android') {
    // Use base64 - file does not work on Android
    path = `data:text/csv;base64,${new Buffer(rows).toString("base64")}`;
  } else {
    // Write the file - base64 does not work on iOS
    path = `${RNFS.TemporaryDirectoryPath}/${filename}.csv`;
    await RNFS.writeFile(path, rows, 'utf8');
  }

  // Temp fix for this issue 1405
  Platform.OS === 'android' && setExporting(false);

  // Share file
  await Share.open({
    url: path,
    failOnCancel: false,
    subject: `MyApp - Export Data - ${filename}`,
  });

  // Post share - never hit
} catch (error) {
  // Handle error - never hit
}

setExporting(false);
ncote3 commented 1 year ago

@robinsoncol I was able to reproduce the issue with the following code. I call this function to initiate the share. The issue happens if I share the file with other apps (E.g. Gmail) or not (by click outside of the action sheet).

This only happens on Android. iOS is fine.

import Share from 'react-native-share';
import RNFS from 'react-native-fs';
import { Buffer } from 'buffer';

// ...

// I am using exporting to control spinner and disable button
setExporting(true);
try {
  const rows = 'column1,column2,column3\nr1c1,r1c2,r1c3\n,r2c1,r2c2,r2c3\nr3c1,r3c2,r3c3';

  const filename = `${dayjs().format('MMDDYYYYTHHmmss')}`;
  let path = '';
  if (Platform.OS === 'android') {
    // Use base64 - file does not work on Android
    path = `data:text/csv;base64,${new Buffer(rows).toString("base64")}`;
  } else {
    // Write the file - base64 does not work on iOS
    path = `${RNFS.TemporaryDirectoryPath}/${filename}.csv`;
    await RNFS.writeFile(path, rows, 'utf8');
  }

  // Temp fix for this issue 1405
  Platform.OS === 'android' && setExporting(false);

  // Share file
  await Share.open({
    url: path,
    failOnCancel: false,
    subject: `MyApp - Export Data - ${filename}`,
  });

  // Post share - never hit
} catch (error) {
  // Handle error - never hit
}

setExporting(false);

This is exactly what is happening in my environment. I have not tested on iOS, but the promise is never getting resolved on android. My code is very similar to yours.

Running a Samsung Galaxy on Android 12. Using react-native-share: 9.2.3

Any advice or guidance would be appreciated!

guistrutzki commented 1 year ago

I'm also having this issue. I have a loading layer controlled by a state and I can't hide it. Any of try, catch or finally blocks aren't resolving the promise after executing Share.open(), so the app get stuck.

This only happens on Android, in iOS it works fine. Using react-native-share: 9.2.3

Edit: The react-native-share: 8.2.2 is working as expected. While the fix for this version is not addressed, I'll downgrade it.

ncote3 commented 1 year ago

Down grading to react-native-share: 8.2.2 causes Share.open() to always throw regardless of success or rejection. While this at least resolves the promise, it unfortunately causes false error reporting.

quoctruongkt commented 1 year ago

Have there been any solutions for this issue yet? I am using react-native-share@9.2.3

MateusAndrade commented 1 year ago

:tada: This issue has been resolved in version 9.2.4 :tada:

The release is available on:

Your semantic-release bot :package::rocket: