react-native icon indicating copy to clipboard operation
react-native copied to clipboard

WebView: Add an option to load local resource from the app bundle

Open pilwon opened this issue 11 years ago • 56 comments

<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)

pilwon avatar Mar 30 '15 18:03 pilwon

:+1:

AbeEstrada avatar Mar 30 '15 22:03 AbeEstrada

+1

adriansdev avatar Apr 03 '15 22:04 adriansdev

WebView now has an html property:

https://github.com/facebook/react-native/blob/master/Libraries/Components/WebView/WebView.ios.js#L84

nicklockwood avatar Apr 27 '15 22:04 nicklockwood

@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.

pilwon avatar Apr 27 '15 22:04 pilwon

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 avatar Apr 27 '15 23:04 nicklockwood

Let me know if that isn't working, and I'll re-open.

nicklockwood avatar Apr 27 '15 23:04 nicklockwood

@nicklockwood It works! :scream_cat::scream_cat::scream_cat: This is the best solution for this issue :metal: Thanks-

pilwon avatar Apr 27 '15 23:04 pilwon

@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 avatar May 10 '15 10:05 mydearxym

@mydearxym Local files must also be compiled into the bundle. See step #2

carbonphyber avatar Sep 04 '15 08:09 carbonphyber

@carbonphyber @nicklockwood I am unable to load html file using query params example "index.html?data=hello" :( Thanks

gs-akhan avatar Nov 12 '15 19:11 gs-akhan

@gs-akhan Yeah, that won't work - file paths can't have GET params.

nicklockwood avatar Nov 12 '15 22:11 nicklockwood

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

DaveAdams88 avatar Feb 03 '16 11:02 DaveAdams88

@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 avatar Feb 03 '16 11:02 nicklockwood

@nicklockwood Not sure. I'll check it out on Monday

dmmiller avatar Feb 05 '16 12:02 dmmiller

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.

nicklockwood avatar Feb 05 '16 13:02 nicklockwood

That's awesome! thanks @nicklockwood! :)

DaveAdams88 avatar Feb 07 '16 14:02 DaveAdams88

@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 avatar Mar 03 '16 14:03 jaulz

@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 avatar Mar 03 '16 15:03 nicklockwood

@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 avatar Mar 03 '16 15:03 jaulz

@jaulz once you have the uri, you can load it via XMLHttpRequest.

nicklockwood avatar Mar 03 '16 16:03 nicklockwood