alinz / react-native-webview-bridge

React Native Webview with Javascript Bridge
MIT License
1.37k stars 491 forks source link

Inserted web js not working #170

Open Andrew-Tan opened 7 years ago

Andrew-Tan commented 7 years ago

Hi,

I am intended to use webview bridge to display a local html page. When I try injecting the script through webview bridge, everything works. However, then I try putting the same script directly into the html page through tag, it doesn't seems to work.

Any hints? Thanks

jqn commented 7 years ago

I'm having the same problem +1

xiewang commented 7 years ago

+1

xiewang commented 7 years ago

In the debug mode, it works fine, but not in release. I find that in debug mode, webView load js files from http://localhost:8081, in release mode, webview can only load js files from bundle resources. But, even I add the js files to the 'Copy Bundle Resources', it still not works.

So, I do these changes:

  1. Change the html file to string const html = `<html>...</html>`
  2. Just transfer the html to vewView

        <WebViewBridge ref="webviewbridge" source={{html:html}}>
    
        </WebViewBridge>
  3. Change the code in RCTWebViewBridge.m

    if (html) {
      NSURL *baseURL = [RCTConvert NSURL:source[@"baseUrl"]];
      [_webView loadHTMLString:html baseURL:baseURL];
      return;
    }

    To

    if (html) {
        NSURL *url = [NSURL fileURLWithPath: [[NSBundle mainBundle]             resourcePath] isDirectory: YES];
        [_webView loadHTMLString:html baseURL:url];
      return;
    }
  4. add the local js files to 'Copy Bundle Resources'.

Then, it works fines now.