Giter Club home page Giter Club logo

react-native-webview-messaging's Introduction

React Native WebView Messaging

React Native WebView extension with 2-way event-based communication API

GitHub stars Twitter Follow

⚠️ this is v2 branch which is not yet stable. Check out v1 branch for v1 docs

Installation

npm install react-native-webview-messaging@next

Examples

Examples

Usage

React Native

import React, { Component } from 'react';
import { View, TouchableHighlight } from 'react-native';
import { connectToRemote, WebView } from 'react-native-webview-messaging';

export class ExampleView extends Component {
  render() {
    return (
      <View>
        <WebView
          ref={ webview => { this.webview = webview; }}
          source={ require('./some-page.html') }
        />
        <TouchableHighlight onPress={this.sendMessageToWebView} underlayColor='transparent'>
          <Text>Send message to WebView</Text>
        </TouchableHighlight>
      </View>
    )
  }

  async componentDidMount() {
    this.remote = await connectToRemote(this.webview);

    this.remote.on('text', text => console.log(text));
    this.remote.on('json', json => console.log(json));
    this.remote.on('custom-event-from-webview', eventData => console.log(eventData));
  }

  sendMessageToWebView = () => {
    this.remote.sendJSON({
      payload: 'JSON from RN'
    });

    this.remote.send('plain text from RN');

    this.remote.emit('custom-event-from-rn', { payload: 'Custom event from RN' });
  }
}

Web

import { connectToRemote } from 'react-native-webview-messaging/web';

connectToRemote()
  .then(remote => {
    remote.on('text', text => console.log(text));
    remote.on('json', json => console.log(json));
    remote.on('custom-event-from-rn', data => console.log(data));

    remote.send('plain text from WebView');
    remote.sendJSON({
      payload: 'JSON from WebView'
    });

    remote.emit('custom-event-from-webview', { payload: 'Custom event from WebView' });
  });

LICENSE

MIT

GitHub stars Twitter Follow

react-native-webview-messaging's People

Contributors

dave-irvine avatar gaboesquivel avatar lesnitsky avatar reggie3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-native-webview-messaging's Issues

onPermissionRequest

I'm guessing because you are basically superclassing the native WebView, this component is also susceptible to this issue: facebook/react-native#11610 ?

Any chance you could extend your implementation somehow to allow this?

"Module JSTimersExecution is not a registered callable module (calling callTimers)" Error

I'm getting the following error when trying to run the expo example using Expo version 20:
screenshot_20170820-133604

I've updated the package.json file as directed in this post from Expo.io https://blog.expo.io/expo-sdk-v20-0-0-is-now-available-79f84232a9d1

resulting in a package.json file that looks like this:

{
  "name": "react-native-webview-messaging_example",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "jest-expo": "~20.0.0",
    "react-native-scripts": "1.1.0",
    "react-test-renderer": "16.0.0-alpha.6"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "node node_modules/jest/bin/jest.js --watch",
    "build-web": "webpack --progress"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "babel-loader": "^7.1.1",
    "babel-preset-env": "^1.6.0",
    "expo": "^20.0.0",
    "html-webpack-inline-source-plugin": "^0.0.9",
    "html-webpack-plugin": "^2.28.0",
    "react": "16.0.0-alpha.12",
    "react-native": "https://github.com/expo/react-native/archive/sdk-20.0.0.tar.gz",
    "react-native-webview-messaging": "^1.1.0",
    "webpack": "^2.6.1"
  }
}

I'm using a Motorola Nexus 6 as the test device, and I have successfully run Expo/react-native projects previously on my current development setup.

Event call on Android but not on iOS

I'm trying to figure out how to call an event on iOS, it works fine on Android, but I'm not sure why it doesn't on iOS, is there anything to add in the code or in the iOS project in order to make it work?

Thanks!

await connectToRemote() not returning a connection or error

The following code doesn't return a connection, nor does connectToRemote() return an error to be caught

 makeRemoteConnection = async () => {
    this.printElement('connecting to remote');
    try {
      let remote = await connectToRemote();
      this.remote = remote;
      this.printElement('remote connected');
      this.printElement(this.remote);
      this.bindListeners();

      // let the webview know we are listening
      this.printElement('emitting webview ready');
      this.remote.emit('WEBVIEW_READY', {
        payload: 'hello'
      });
      this.printElement('WEBVIEW_READY emitted');
    } catch (error) {
      console.log(`Error: ${error}`);
      this.printElement(`remote connect error ${error}`);
    }
  };

I receive the "connecting to remote" message, but I never receive the expected "remote connected" message. Additionally, no error is thrown for catching.

Can you provide a working HTML?

in the example folder, you refer to './dist/index.html' and that does not exist. I am curious to use this. Can you please provide an example HTML

file miss

Platform

  • Android
  • iOS

What react-native version are you using?

Issue details

image

thanks for the package, but after I clone it to my computer,I found BasicExample.html is inexistence

Unexpected token / in JSON at postion 0

Receiving the following error when using "braintree-web-drop-in" module in react native webview.
screenshot_20170825-171546

The error happens when filling in a submit box on the form that the braintree plug in creates.
The code in the webview index.tpl.html is as follows:

import RNMessageChannel from "./react-native-webview-messaging";
const dropin = require("braintree-web-drop-in");

const button = document.querySelector("#submit-button");
let clientToken = "";

RNMessageChannel.on("json", json => {
  clientToken = json.clientToken;

  dropin
    .create({
      authorization: clientToken,
      container: "#dropin-container"
    })
    .then(instance => {
      // alert(`instance: ${JSON.stringify(instance)}`);
      button.addEventListener("click", function() {
        instance.requestPaymentMethod(function(err, payload) {
          // Submit payload.nonce to your server
            //alert(`payload: ${JSON.stringify(payload)}`);
        });
      });
    })
    .catch(function(err) {
      // Handle any errors that might've occurred when creating Drop-in
      console.error(err);
    });
});

I've gotten the react-native-webview-messaging example expo project to work. It seems like braintree's library is sending an emit upon form update that I would like react-native-webview-messaging to ignore. Unfortunately, I can't create a working "console log" or "alert" inside the RNWM's emit function to see what is being set to it. I think that if I can see what is being set to it, I can figure out some way to ignore it. Do you have any ideas that would give me insight into what is being emitted.

Doesn't seem to work on iOS 9.3

Installed and ran the example, both the native and the webview are shown, but clinking the links in native and clicking the buttons in the webview both do nothing.

Works fine in 10.2

(V1) Cannot run on Android

I try to run

npm run android

But it returned error.

undefined is not a function (evaluating
'ReactComponentTreeHook.purgeUnmountedComponent()')

Android Javascript injection not working.

Hey thanks for the cool module its working perfectly for us on IOS.
However its not working on Android.
We have tried multiples devices/simulators and even after manually adding the injected code to window its still not functioning. I tried loading it in various places in my code and even adding it to the Web view directly but it seems its not working at all. This might be related to react-native however. Did you have any experience like this?

webview messagesChannel not working on event listener callback

Platform

  • Android
  • iOS

What react-native version are you using?

0.51

Issue details

I am sending a message to the webview on a keyboard event listener

    componentWillMount() {

        this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.keyboardDidShow.bind(this));

    }

The thing is that the method keyboardDidShow is receiving the webview but the on method is not working on the webview side.

If I log the emit this.webview.messagesChannel.emit('toggleKeyboardNative') I get returned false

Other emit calls works in the webview side with the on receivers.

react-native bundling fails when `require`-ing a html file

I have no idea why this works in your Expo example but not in my normal react-native project, but when I do

<WebView source={require('./webview.html')} style={{ flex: 1, borderWidth: 2, borderColor: 'red' }} ref={this._refWebView}/>

I get the following error:

Bundling index.ios.js 0.0% (0/1), failed.
SyntaxError:
../src/components/Header/components/webview.html: Unexpected token (1:1)

1 | <!DOCTYPE html>
  |  ^
2 | <html lang="en">
3 | <head>
4 |   <meta charset="UTF-8">

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.