alinz / react-native-webview-bridge

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

Fix iOS imports on RN0.40+ and replace UIWebview with WkWebview #217

Open guilhermebruzzi opened 7 years ago

guilhermebruzzi commented 7 years ago

No more deprecated webview on iOS and let people use webviewbridge API with RN 0.40+ I also included a new example SampleRN42 that shows that the main features still work.

fungilation commented 7 years ago

Wow, moving to WKWebview is a big jump. Which I wanted.

Are you using it in your own app and tested? Does it maintain same API as before, more or less?

guilhermebruzzi commented 7 years ago

@fungilation Are you using it in your own app and tested? <- Yes Does it maintain same API as before, more or less? <- Yes, only scalesPageToFit is not supported by wkwebview (all other props we used worked well)

fungilation commented 7 years ago

I commented inline in code:

Any reason why this isn't changed to #import <React/UIView+React.h>?

Same in ios/RCTWebViewBridge.m

guilhermebruzzi commented 7 years ago

@fungilation No reason, sent a fix, TY! :)

fungilation commented 7 years ago

Switched to your PR in my app, no bug so far that I can see. With noticeable improvement in WebViews' loading performance, much thanks for this!

guilhermebruzzi commented 7 years ago

@fungilation Nice! :D

peterept commented 7 years ago

The injectedJavaScript property no longer works.

Do you have a good idea how to add that back in for WkWebView ?

fungilation commented 7 years ago

I confirm that the WKWebView fork never worked. I mistakenly had the old version in my test app. On linking the new fork, app just crash to home screen on WebView's render.

I am using and need the injectedJavaScript prop.

peterept commented 7 years ago

@fungilation it mostly works for me with a few tweaks (which were ok for my situation):

<script>
 (function () {
    function onWebViewBridgeReady() {
      document.removeEventListener('WebViewBridge', onWebViewBridgeReady, false);
      WebViewBridge.onMessage = function (message) {
          // assume message has a color 
          document.body.style.backgroundColor = message;
        }
     }
      WebViewBridge.send("webviewbridge-ready");
    }
   document.addEventListener('WebViewBridge', onWebViewBridgeReady, false)
  }())
</script>

My document works with that and I can send/receive data over the bridge.

guilhermebruzzi commented 7 years ago

@fungilation @peterept I fixed now this injectedJavaScript property and created a new example: examples/SampleRN42/ that you can use to test

guilhermebruzzi commented 7 years ago

@fungilation @peterept Anyway I never used injectedJavaScript in my projects. I always embedded in the document like @peterept said, because is way faster and should be your solution if you have control over the document

fungilation commented 7 years ago

For me I need to inject JS, as I'm loading external websites. Thanks @guilhermebruzzi

peterept commented 7 years ago

Right. The code needs to use -[WKWebView evaluateJavaScript:completionHandler:] to do the inject, but I'm not sure where react-native-webview-bridge needs to do the injection exactly.

guilhermebruzzi commented 7 years ago

@peterept I called the method on the same places that UIWebview used to call his inject method. Even If you call It earlier, the webview (on Android too) only allows the code to run after it has finished loading (If i'm not mistaken)

peterept commented 7 years ago

holy moley @guilhermebruzzi . I'm a fool... I cloned from your master branch on your fork (which has an older version of your code with WkWebView - missing the injection code). I'll switch to your branch feature/wkwebview and report back!

peterept commented 7 years ago

Confirmed it works! That's awesome work @guilhermebruzzi.

I installed from the correct branch (although I see you've updated master now also):

$ npm install --save git://github.com/vtex/react-native-webview-bridge.git#feature/wkwebview
$ react-native run-ios

And now the injectedJavaScript={injectScript} works and also the WebViewBridge variable is set already (as in the sample code) and so no need to wait for it:

const injectScript = `
(function () {
  if (WebViewBridge) {
    document.body.style.backgroundColor = "red";
  }
}());
`;

Thank you so much for this.

guilhermebruzzi commented 7 years ago

@peterept no problem! :) Yeah, you can install from master branch also.

iOSHw commented 7 years ago

@guilhermebruzzi how to use this pull request ? the master branch have problem!

guilhermebruzzi commented 7 years ago

@iOSHw What kind of problem?

To use the master branch you insert:

"react-native-webview-bridge": "vtex/react-native-webview-bridge",

on package.json dependencies

And than run npm install and react-native link

UnsignedInt8 commented 7 years ago

@guilhermebruzzi Hi, bro. I really appreciate your hard working, but it seems not working under RN 0.44.2. Can you fix it? screen shot 2017-05-28 at 16 15 48

jenskuhrjorgensen commented 7 years ago

Awesome! I couldn't get this library to work ("Cannot read property 'NavigationType' of undefined") until I installed this PR - THANKS! :D

jenskuhrjorgensen commented 6 years ago

@guilhermebruzzi @alinz is anybody working on fixing the conflicts and merging this branch into master?

alinz commented 6 years ago

@jenskuhrjorgensen I'm extremely busy that's why I'm constantly looking for good maintainer? Are you interested?

jenskuhrjorgensen commented 6 years ago

@alinz That's a shame - your library has been very useful to me! :) I'm afraid I don't have the time either.