React Native’s WebView gives us an sparkling facility to Load Local HTML File with Style.CSS and JavaScript file inside WebView in Android and iOS applications. We can make a Mobile website in HTML language with CSS file and put that complete website inside our Application in a particular folder and call the specific HTML file in WebView. This would help us to create and Show Static website application for some special purpose. All the Tags in HTML file should work fine and this method also supports External CSS(Caching Style Sheet) and JavaScript + JQuery files attached to HTML file.
Contents in this project Load Local HTML File Place Inside App in WebView in Android iOS Example :
1. Create a folder inside your project folder named as Local_Website like i did in below screenshot.
2. Now Download Both SampleHtmlFile.html and Style.css file from below and copy inside Local_Website folder.
Download SampleHtmlFile.html File.
Code For SampleHtmlFile.html file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<HTML>
<HEAD>
<link rel=“stylesheet” href=“styles.css”>
<TITLE> Sample Web Page </TITLE>
</HEAD>
<BODY>
<H1> THIS IS SAMPLE HEADING IN WEBPAGE. </H1>
<p> Sample Paragraph Tag Text in HTML File. </p>
</BODY>
</HTML>
|
Code for styles.css file :
1
2
3
4
5
6
7
8
9
|
body {
background–color: #00BCD4;
}
h1 {
color: #fff;
}
p {
color: #fff;
}
|
Now next step is to start coding your application.
3. Open your App.js file and Import StyleSheet, WebView and Platform component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { StyleSheet, WebView, Platform} from ‘react-native’;
|
4. Create a const variable named as LoacalWebURL and set the Web page URL using require format.
1
|
const LoacalWebURL = require(‘./Local_Website/SampleHtmlFile.html’);
|
5. Create the WebView component inside render’s return block and set Source as LoacalWebURL . This would directly navigate the Source to local webpage.
1
2
3
4
5
6
7
8
9
10
11
12
|
render() {
return (
<WebView
style={styles.WebViewStyle}
source={LoacalWebURL}
javaScriptEnabled={true}
domStorageEnabled={true} />
);
}
|
6. Create Custom Style.
1
2
3
4
5
6
7
8
9
10
11
|
const styles = StyleSheet.create(
{
WebViewStyle:
{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
marginTop: (Platform.OS) === ‘ios’ ? 20 : 0
}
});
|
7. Complete source code for App.js File :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import React, { Component } from ‘react’;
import { StyleSheet, WebView, Platform} from ‘react-native’;
const LoacalWebURL = require(‘./Local_Website/SampleHtmlFile.html’);
export default class MainActivity extends Component {
render() {
return (
<WebView
style={styles.WebViewStyle}
source={LoacalWebURL}
javaScriptEnabled={true}
domStorageEnabled={true} />
);
}
}
const styles = StyleSheet.create(
{
WebViewStyle:
{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
marginTop: (Platform.OS) === ‘ios’ ? 20 : 0
}
});
|
Screenshot in Android application :
Screenshot in iOS Application :