<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Obeya Edward Obande</title>
    <description>The latest articles on DEV Community by Obeya Edward Obande (@obie3).</description>
    <link>https://dev.to/obie3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F472668%2Ff4a64e1d-6158-4ede-bd39-b783127674f2.jpeg</url>
      <title>DEV Community: Obeya Edward Obande</title>
      <link>https://dev.to/obie3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/obie3"/>
    <language>en</language>
    <item>
      <title>Download PDF files to device storage with React Native Webview</title>
      <dc:creator>Obeya Edward Obande</dc:creator>
      <pubDate>Sun, 15 Aug 2021 10:12:59 +0000</pubDate>
      <link>https://dev.to/obie3/download-pdf-files-to-device-storage-with-react-native-webview-2i2j</link>
      <guid>https://dev.to/obie3/download-pdf-files-to-device-storage-with-react-native-webview-2i2j</guid>
      <description>&lt;p&gt;According to Google search, React Native WebView is a modern, well-supported, and cross-platform WebView for React Native. It is intended to be a replacement for the built-in WebView (which will be removed from core).&lt;/p&gt;

&lt;p&gt;Its a component built to enable RN app developers render websites or HyperText Markup Language (HTML) in your RN app, giving the end users nearly the same experience as visiting the website on their native mobile browsers like chrome, Mozilla fire-fox etc  without leaving the application thereby improving user experience. &lt;/p&gt;

&lt;p&gt;Though it has some limitations like handing file download on apple devices which we shall be looking at shortly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting started with React Native WebView
&lt;/h3&gt;

&lt;p&gt;First you will need to install the library with the command below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// with yarn
$ yarn add react-native-webview

// with npm
$ npm install --save react-native-webview
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;please refer to the &lt;a href="https://github.com/react-native-webview/react-native-webview/blob/master/docs/Getting-Started.md"&gt;official installation guide&lt;/a&gt; for further step.&lt;/p&gt;

&lt;p&gt;I hope you were able to install it  successfully at this point.&lt;/p&gt;

&lt;p&gt;Now I will show how to download PDF files to different directories on both iOS and android.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic quickstart with WebView
&lt;/h3&gt;

&lt;p&gt;We will start with the most basic props as we proceed we will add more props from the official documentation where necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';
import {View, Share} from 'react-native';
import * as FileSystem from 'expo-file-system';
import { WebView } from 'react-native-webview';
const { downloadAsync, documentDirectory } = FileSystem;


class MyWeb extends Component {
  render() {
    return (
      &amp;lt;WebView
        source={{ uri: 'https://google.com/' }}
        style={{ marginTop: 20 }}
      /&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The source props is a required props that is used to load the web content into the view when its rendered, it could be valid website content loaded over https:// or a local html file defined within the project but for we will focus on downloading files locally for more details on how to load local webpages you can visit &lt;a href="https://blog.logrocket.com/the-complete-guide-to-react-native-webview/"&gt;LogRocket.com&lt;/a&gt;, the have an amazing content on it.&lt;/p&gt;

&lt;p&gt;Next we shall be adding additional props of Webview and code  to enable us download file.&lt;/p&gt;

&lt;p&gt;React Native Webview component has &lt;a href="https://github.com/react-native-webview/react-native-webview/blob/cb1c3f459f4ccdacec3123f9379d722df1a37e98/docs/Reference.md#onFileDownload"&gt;onFileDownload props&lt;/a&gt; that handles file download for ios alone which we will implement shortly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       &amp;lt;WebView
        source={{
          uri: 'https://www.google.com',
        }}
        ref={webviewRef}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        allowFileAccess={true}
        allowUniversalAccessFromFileURLs={true}
        allowingReadAccessToURL={true}
        mixedContentMode={'always'}
        onFileDownload={({ nativeEvent: { downloadUrl } }) =&amp;gt;
          downloadDocument(downloadUrl);
        }
      /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;downloadDocument function&lt;/strong&gt; inside the onFileDownload props callback accepts a param which is the url of the pdf file, downloads it to the app private storage on the device using expo file system, you can use other React Native libraries like &lt;strong&gt;react-native-fs&lt;/strong&gt;, &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/react-native-fetch-blob"&gt;react-native-fetch-blob&lt;/a&gt;&lt;/strong&gt; that supports file download out of the box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let downloadDocument = async (downloadUrl) =&amp;gt; {
    let fileURI = await downloadAsync(
      downloadUrl,
      `${documentDirectory}name.pdf`,
      {},
    );
    await onShare(fileURI.uri);
  };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we shall define &lt;strong&gt;onShare function&lt;/strong&gt; that will help us suggest suitable locations like files directory on ios to save our pdf file downloaded and we are done&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const onShare = async (url) =&amp;gt; {
    try {
      return Share.share({
        message: 'Choose location to save pdf file',
        url: url,
      });
    } catch (error) {
      return error;
    }
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note the following: &lt;/p&gt;

&lt;p&gt;The onFileDownload props is compatible only with ios, it wont work on android.&lt;/p&gt;

&lt;p&gt;Android Download Manager handles every webview download out of the box without much tweak or config other than &lt;strong&gt;Write to External permission&lt;/strong&gt; that must be requested.&lt;/p&gt;

&lt;p&gt;The Content-type of the downlaod url should be set to &lt;strong&gt;application/pdf&lt;/strong&gt; in the header config of the server where the file is hosted other wise the downloaded file will have &lt;strong&gt;.bin ext&lt;/strong&gt; on android &lt;/p&gt;

&lt;p&gt;The code snippet was tested and worked for pdf files both on android/ios devices, to download png/jpeg/jpg please use media storage instead.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>reactnative</category>
      <category>webview</category>
    </item>
  </channel>
</rss>
