4

Need to write a file(pdf) and store it in device memory. I'm using react-native-fs library.

I want to make users able to read this file using iBooks as well.

These are react-native-fs 's constants

  • MainBundlePath (String) The absolute path to the main bundle directory
  • CachesDirectoryPath (String) The absolute path to the caches directory
  • DocumentDirectoryPath (String) The absolute path to the document directory

  • TemporaryDirectoryPath (String) The absolute path to the temporary directory (iOS only)

this is my download code

  RNFS.downloadFile({
      fromUrl: fileLink,          
      toFile: RNFS.MainBundlePath + '/' + fileName
    }).promise.then((dwResult) => {

      console.log(dwResult.jobId + 'jobid');
      return true
    }).catch((err) => {

      alert(err);
      return false
    })
  }

What do I have to assign toFile so that written file can be seen using iBooks. Note: I'm able to write file app's path using DocumentDirectoryPath.

Burak Karasoy
  • 1,682
  • 2
  • 21
  • 33

1 Answers1

2

There're some restrictions on iOS so you may not be able to open a file directly using iBooks. However, latest beta version of react-native-fetch-blob has an API which displays an option menu and let user decide how to deal with the file.

Alternatively, you can directly preview the file with default document viewer, there's also a Share Button for display the option menu

An example for download and open a PDF file with react-native-fetch-blob

  RNFetchBlob.config({
    fileCache : true,
    appendExt : 'pdf'
  })
  .fetch('GET',`${TEST_SERVER_URL}/public/book.pdf`)
  .then((res) => {
    // open the document directly
    RNFetchBlob.ios.previewDocument(res.path())
    // or show options
    // RNFetchBlob.ios.openDocument(res.path())
  })
Xeijp
  • 853
  • 1
  • 7
  • 18
  • You mean if i write or download a file using my application, i can't preview it using ibooks or something. I want to preview them when my app is closed. İsn't that possible? – Burak Karasoy Oct 12 '16 at 15:23