You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WebView: Add an option to load local resource from the app bundle #505
<WebView/> currently only supports url prop to control what content to load in the web view.
We need an option to load local resource (mainly html but could be others too) from the application bundle.
We could either introduce a new prop for that or reuse url and do some magic conversion. For example, local://<path>.<ext> can be automatically converted to file:///<converted-url>.
@nicklockwoodWhat kind of string values does html prop expect? Looks like you closed a wrong issue. The above link points at a line modified by the commit 4b4e8ec and it partially fixes #506, not #505. Can we reopen this issue? I am afraid we are not ready to close #506 as well.
Oh, sorry, quite right. But as it happens, any relative URL is considered to be relative to the app resource bundle, so <WebView url="foo.html" /> should load a file called foo.html that's been added to the project.
Reacted by Jonathan Palma, Jane Sheng, Trip Shanti, Nathan Broadbent and Craignewkirk
This works great in iOS, but having trouble getting it to work with android - I've put the web view in "android/app/src/main/assets/" - and using "source='webview.html'" - is there a step I am missing? .. or am I flat out doing it wrong? :s
@dmmiller, do relative paths work the same way in Android as they do in iOS? Can you use "foo.html" as a shorthand for "android/app/src/main/assets/foo.html" ?
Is it correct to use source={{uri: path_to_file}} for iOS app?
If yes, than what url format should I use. I mean in android I'm using 'file:///android_asset/path_to_file', but what in iOS?
But if I don't want to do it manually in Xcode. For instance I want to add some files to the app bundle using some script before making a build, and then I want to require one of those files for showing WebView (when the app is working)
I guess there is no way to include some file to the WebView on the fly on iOS.
Because require('path_to_file') doesn't support dynamic urls
and {uri: 'path_to_file'} requires to add files manually using Xcode
I found the function getDevServerURL and getBundleSourceURL from resolveAssetSource.js, It's helpfull.
And I create my workaround solution , it worked in both dev and production mode
functiongetBundleSourceURL(): ?string{if(_bundleSourceURL===undefined){constscriptURL=NativeModules.SourceCode.scriptURL;if(!scriptURL){// scriptURL is falsy, we have nothing to go on here_bundleSourceURL=null;return_bundleSourceURL;}letmatch=scriptURL&&scriptURL.match(/^https?:\/\/.*?\//);if(match){// Bundle was loaded from network_bundleSourceURL=match[0];return_bundleSourceURL;}_bundleSourceURL=scriptURL.substring(0,scriptURL.lastIndexOf('/')+1);if(scriptURL.startsWith('assets://')){// android: running from within assets, no offline path to use_bundleSourceURL=_bundleSourceURL.replace('assets://',"file:///android_asset/");return_bundleSourceURL;}if(!scriptURL.startsWith('file://')){// Add file protocol in case we have an absolute file path and not a URL.// This shouldn't really be necessary. scriptURL should be a URL._bundleSourceURL='file://'+_bundleSourceURL;}}return_bundleSourceURL;}exportdefaultclassWebPageextendsComponent<{}>{
static navigationOptions=({navigation})=>{return{title: `${navigation.state.params.title}`,}};render(){let{source}=this.props.navigation.state.params;if(typeof(source)==='string'){if(!source.match(/^.*:\/\/.*?/)){if(getBundleSourceURL()){source=getBundleSourceURL()+source;}}source={uri: source};}return(<Viewstyle={{flex:1}}><WebViewsource={source}/></View>);}}
hey guys
found the trick base on Image doesn't show after bundling into the apk.
as you know require('index.html') work on simulator and emulator for both &driod and IOS
but problem will faced when you bundle and you want release your app
for android just follow this enablejavascript and copy you files in assets folder in YOUR_APP/app/src/main/assets and use just picture below.
and for ios just bundle package with react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios
when bundle created you have main.bundle.js and asset folder
just copy you files in .xcworkspace like this image
and set free all refrence in will be and unchanged just like mine.
i hope this tutorial help you from confusing release app in RN
Activity
AbeEstrada commentedon Mar 30, 2015
👍
adriansdev commentedon Apr 3, 2015
+1
nicklockwood commentedon Apr 27, 2015
WebView now has an html property:
https://github.com/facebook/react-native/blob/master/Libraries/Components/WebView/WebView.ios.js#L84
pilwon commentedon Apr 27, 2015
@nicklockwood
What kind of string values doesLooks like you closed a wrong issue. The above link points at a line modified by the commit 4b4e8ec and it partially fixes #506, not #505. Can we reopen this issue? I am afraid we are not ready to close #506 as well.htmlprop expect?nicklockwood commentedon Apr 27, 2015
Oh, sorry, quite right. But as it happens, any relative URL is considered to be relative to the app resource bundle, so
<WebView url="foo.html" />should load a file called foo.html that's been added to the project.nicklockwood commentedon Apr 27, 2015
Let me know if that isn't working, and I'll re-open.
pilwon commentedon Apr 27, 2015
@nicklockwood It works! 🙀🙀🙀 This is the best solution for this issue 🤘 Thanks-
mydearxym commentedon May 10, 2015
@nicklockwood i use the method
<WebView url="test.html" />and it show :and i tried html option too , not work either~
carbonphyber commentedon Sep 4, 2015
@mydearxym Local files must also be compiled into the bundle.
See step #2
gs-akhan commentedon Nov 12, 2015
@carbonphyber @nicklockwood I am unable to load html file using query params
example "index.html?data=hello"
:(
Thanks
nicklockwood commentedon Nov 12, 2015
@gs-akhan Yeah, that won't work - file paths can't have GET params.
DaveAdams88 commentedon Feb 3, 2016
This works great in iOS, but having trouble getting it to work with android - I've put the web view in "android/app/src/main/assets/" - and using "source='webview.html'" - is there a step I am missing? .. or am I flat out doing it wrong? :s
nicklockwood commentedon Feb 3, 2016
@dmmiller, do relative paths work the same way in Android as they do in iOS? Can you use "foo.html" as a shorthand for "android/app/src/main/assets/foo.html" ?
dmmiller commentedon Feb 5, 2016
@nicklockwood Not sure. I'll check it out on Monday
29 remaining items
kaykaul commentedon Mar 30, 2017
How can I load local css file into web view's html?
dijimon commentedon Apr 20, 2017
Is it correct to use source={{uri: path_to_file}} for iOS app?
If yes, than what url format should I use. I mean in android I'm using 'file:///android_asset/path_to_file', but what in iOS?
mishuagopian commentedon Apr 20, 2017
@dijimon in iOS you have to link the file to your project as it is specified in this guide, in which the author links fonts.
Once you've done that, you can do something like the following when rendering the
WebView:Remember importing
PlatformandWebViewas the following:dijimon commentedon Apr 20, 2017
But if I don't want to do it manually in Xcode. For instance I want to add some files to the app bundle using some script before making a build, and then I want to require one of those files for showing WebView (when the app is working)
mishuagopian commentedon Apr 20, 2017
Maybe you can do the link programatically, but I've never than that 👎
dijimon commentedon Apr 20, 2017
I guess there is no way to include some file to the WebView on the fly on iOS.
Because require('path_to_file') doesn't support dynamic urls
and {uri: 'path_to_file'} requires to add files manually using Xcode
gs-akhan commentedon Apr 23, 2017
@dijimon What is the use case you want to add dynamic files ? May be I can help
Thanks
shaunsaker commentedon Oct 4, 2017
@Dryymoon Too legit, thank you!
dayongxie commentedon Nov 15, 2017
I found the function getDevServerURL and getBundleSourceURL from resolveAssetSource.js, It's helpfull.
And I create my workaround solution , it worked in both dev and production mode
vvavepacket commentedon Jan 2, 2018
what is the equivalent android/app/src/main/assets for iOS?
sujitpk-perennial commentedon Feb 12, 2018
@vvavepacket need to create main.jsbundle. Check this #4084
arash-hacker commentedon Apr 24, 2018
hey guys
found the trick base on Image doesn't show after bundling into the apk.
as you know require('index.html') work on simulator and emulator for both &driod and IOS
but problem will faced when you bundle and you want release your app
for android just follow this enablejavascript and copy you files in assets folder in
YOUR_APP/app/src/main/assetsand use just picture below.and for ios just bundle package with


react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ioswhen bundle created you have main.bundle.js and asset folder
just copy you files in .xcworkspace like this image
and set free all refrence in will be and unchanged just like mine.
i hope this tutorial help you from confusing release app in RN
arash-hacker commentedon Apr 24, 2018
worked with rn 0.54 🙏