-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enables communication between the webview and RN
Android devs have been relying for ages on intercepting custom HTTP calls made by the webview, to map them to native actions, [like here](https://www.caphal.com/android/communication-between-application-and-webview/). Say that we have a "close" button in our web interface. We first detect if we're running on web or within a webview -- if on web, we simply call `window.close()`. If we're running within the app things get a bit more complicated, but we can simply call `app://exit` and the app will be able to intercept this call and close the webview for us. Now, this PR adds the ability of doing it on RN. You basically specify a protocol for messages that should be forwarded to RN (ie. `rn`) and then, when your webview loads a resource from `rn://xyz` the `onMessage` callback will be called. You can access the URL etc etc through `event.nativeEvent.url`. Example: ``` javascript let onMessage = (event) => { let {url} = event.nativeEvent; if (url.endsWith('exit')) { BackAndroid.exitApp(); // just sayin'... } } return <Webview source={...} messageProtocol={'myApp'} onMessage={onMessage} > ``` Or something of that sort. Communicating between the webview and RN / the ability to intercept resources loaded within the webview has been already discussed but I feel we need a very simple implementation rather than creating a full-blown messaging pattern which will just hurt (maintainability, complexity) in the long run. Also, my Java is pretty rusty ;-)
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
ReactAndroid/src/main/java/com/facebook/react/views/webview/events/MessageEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.views.webview.events; | ||
|
||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.uimanager.events.Event; | ||
import com.facebook.react.uimanager.events.RCTEventEmitter; | ||
|
||
/** | ||
* Event emitted when there is an error in loading. | ||
*/ | ||
public class MessageEvent extends Event<MessageEvent> { | ||
|
||
public static final String EVENT_NAME = "message"; | ||
private WritableMap mEventData; | ||
|
||
public MessageEvent(int viewId, long timestampMs, WritableMap eventData) { | ||
super(viewId, timestampMs); | ||
mEventData = eventData; | ||
} | ||
|
||
@Override | ||
public String getEventName() { | ||
return EVENT_NAME; | ||
} | ||
|
||
@Override | ||
public boolean canCoalesce() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public short getCoalescingKey() { | ||
// All events for a given view can be coalesced. | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void dispatch(RCTEventEmitter rctEventEmitter) { | ||
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mEventData); | ||
} | ||
} |