facebook / react-native

A framework for building native applications using React

Home Page:https://reactnative.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RN 0.59.8: postMessage result from iOS WebView is double-encoded

aldistel opened this issue · comments

The data returned to onMessage WebView callback on iOS is double-percent-encoded.

React Native version: 0.59.8

Steps To Reproduce

  1. Inject a JS into WebView that calls e.g. window.postMessage('https://test.com')
  2. The onMessage handler will receive double-encoded value:
<WebView onMessage={onMessage} ... />

function onMessage(event) {
   console.log(event.nativeEvent.data);  // "https%253A%252F%252Ftest.com"
}

This was introduced in 61ca119#diff-17ddfccc3e39071c0aa51f82677e6d68 :

image

Correct method to use instead of stringByReplacingPercentEscapesUsingEncoding is stringByRemovingPercentEncoding.

cc @pvinis

React Native's WebView was deprecated please migrate to Community version

me too

I had the same error with Expo SDK 33 and iOS.
So I have tried to use react-native-webview from community, and it works to me!
In this change, if you use postMessage function, you should change windows.postMessage() to window.ReactNativeWebView.postMessage().

However, it shows error message as belows when I inject this code
Error evaluating injectedJavaScript: This is possibly due to an unsupported return type. Try adding true to the end of your injectedJavaScript string..

If you try this and solve, please notice me, too!

me too

me too

改 OC原生可以避免它

Platform.OS === 'ios' ? decodeURIComponent(decodeURIComponent(event.nativeEvent.data)) : event.nativeEvent.data

Platform.OS === 'ios' ? decodeURIComponent(decodeURIComponent(event.nativeEvent.data)) : event.nativeEvent.data

I tried your method, but it still went wrong.

commented

I solve it with

let string_data = m.nativeEvent.data;
if(Platform.OS === "ios"){
	// IOS returns the data url encoded/percent-encoding twice
	// unescape('%257B') -> %7B
	// unescape(%7B) -> {
	string_data = unescape(unescape(string_data));
}

somehow adding useWebKit={true} fix the issue.


seems the issue only affect UIWebView, not WKWebView

somehow adding useWebKit={true} fix the issue

it's working for me, thanks a lot!
(using expo sdk 33 on iOS)

commented

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

commented

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.