WebView: Add an option to load local resource from the app bundle
<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>.
NSURL *convertedURL = [[NSBundle mainBundle] URLForResource:@"<path>" withExtension:@"<ext>"];
(Use case: #497)
[UPDATE]
As @nicklockwood mentioned in his comment https://github.com/facebook/react-native/issues/505#issuecomment-96850121, this works out of box by simply setting a relative file path to url prop. (ex: index.html)
:+1:
+1
WebView now has an html property:
https://github.com/facebook/react-native/blob/master/Libraries/Components/WebView/WebView.ios.js#L84
@nicklockwood ~~What 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 https://github.com/facebook/react-native/commit/4b4e8ecc9bf67b47577f9cfdd52bd1d177c0ab44 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.
Let me know if that isn't working, and I'll re-open.
@nicklockwood It works! :scream_cat::scream_cat::scream_cat: This is the best solution for this issue :metal: Thanks-
@nicklockwood i use the method <WebView url="test.html" /> and it show :
Error setting property 'url' of RCTWebView with tag #507: The file 'test.html -- file:///Users/xieyiming/Library/Developer/CoreSimulator/Devices/283F4434-57…rs/Bundle/Application/72594A59-1805-4D5C-B8AB-AB79958D08B6/phone_side.app/' does not exist
and i tried html option too , not work either~
@mydearxym Local files must also be compiled into the bundle. See step #2
@carbonphyber @nicklockwood I am unable to load html file using query params example "index.html?data=hello" :( Thanks
@gs-akhan Yeah, that won't work - file paths can't have GET params.
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" ?
@nicklockwood Not sure. I'll check it out on Monday
Thanks @dmmiller!
@DaveAdams88 there's another option that I just landed support for in master, which should work cross platform. You can now use require() to load HTML files like you do with images:
<WebView source={require('./foobar.html')}/>
You can see this working in the UIExplorer WebView example. What's great about this is that it serves the file via the packager, so it lives relative to your js, and you can modify it without rebuilding the app.
The downside is that you'll have to embed any CSS, images, etc. inline in the HTML file because the packager won't automatically bundle any files referenced by the HTML unless they are also require()'d in your js code somewhere.
That's awesome! thanks @nicklockwood! :)
@nicklockwood That sounds great! Is it also possible to use it outside of WebView? When I try to require a *.html file I get a simple 1 returned:
console.log(require('test.html'));
> 1
@jaulz require(...) doesn't return the source object directly, it returns a resource id. Try this:
var resolveAssetSource = require('resolveAssetSource');
var source = resolveAssetSource(require('test.html'));
console.log(source.uri);
@nicklockwood Thanks for the quick answer :) Though I hoped that it is possible to get the content of the HTML file as a JavaScript string. I assume this is not possible?
@jaulz once you have the uri, you can load it via XMLHttpRequest.