react-native-file-access icon indicating copy to clipboard operation
react-native-file-access copied to clipboard

cpExternal to 'downloads' works in Android but not in iOS

Open afilp opened this issue 2 years ago • 3 comments

Hello,

While it works in iOS, the cpExternal does not work in iOS. Note that I have both UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace enabled. Testing in emulator. Shouldn't the destination ('downloads') be a "common" folder and not an application-specific folder?

Is this a bug or a non-proper implementation?

Thanks a lot!

image

const fetchPayment = async (paymentId, createdDate) => {
  const token = await getAuthToken();
  console.log('token', token);

  const destPath = `${Dirs.CacheDir}/${createdDate}-Invoice.pdf`;

  // const date = new Date();
  const { data } = await new Promise((resolve, reject) => {
    try {
      FileSystem.fetch(
        `${window.our.BASE_URL}/payment?paymentId=${paymentId}`,
        {
          headers: {
            Authorization: `Bearer ${token}`,
            Accept: 'application/pdf',
            'Content-Type': 'application/pdf',
          },
          path: destPath,
        },
      )
        .then(async res => {
          console.log('res', res);
          await FileSystem.cpExternal(
            destPath,
            `${createdDate}-Invoice.pdf`,
            'downloads',
          );
          Alert.alert(
            t('invoiceDownloaded'),
            t('invoiceDownloadedAndroidFolder'),
          );
          resolve(res);
        })
        // Something went wrong:
        .catch((errorMessage, statusCode) => {
          console.log(errorMessage);
          reject(errorMessage);
        });
    } catch (err) {
      console.log(err);
      reject(err);
    }
  });
  return data;
};

afilp avatar Jan 29 '22 22:01 afilp

File is put where os says it should be. Best as I can tell, that is always sandboxed within the app (might not be true for macos, but I do not use that). Apple procedure for sharing files between apps is by marking the files as sharable (the settings you have enabled). With that, the iOS system file browser should be able to select files from this app for other apps to open.

If you find native api to allow otherwise, let me know.

alpha0010 avatar Jan 31 '22 14:01 alpha0010

Thanks @alpha0010, I am OK to leave the file within the app's sandbox. The problem is that cpExternal throws an error while I am using the expected code. As you see from the message, it just tries to copy from "CacheDir" to "downloads" (which is the app's downloads) and it fails. Am I doing something wrong in the code? I believe I am using just the standard code.

I do not see any app specific folder in the Files app, could this be the case?

afilp avatar Feb 01 '22 21:02 afilp

Oops, sorry, my vision just skipped over the non-English text (or maybe the screenshot did not load for me before?). That is probably a bug.

However, try using cp(), and putting files in Dirs.DocumentDir. I think that should show up in the Files app.

alpha0010 avatar Feb 02 '22 13:02 alpha0010