capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug(docs): triggerWindowJSEvent doesn't allow string data

Open radarsu opened this issue 6 years ago • 7 comments

In iOS documentation under https://capacitor.ionicframework.com/docs/plugins/ios/ there is an error:

self.bridge.triggerWindowJSEvent(eventName: "myCustomEvent", data: "my custom data")

Should be:

self.bridge.triggerWindowJSEvent(eventName: "myCustomEvent", data: "{ 'dataKey': 'dataValue' }")

That cost us quite a bit of time, please fix this, many people had this problem on the Internet.

radarsu avatar Apr 30 '20 13:04 radarsu

The data parameter is of type String, so both examples are correct. I see that the Android version is different, so we could update those for consistency.

1st example: const myData = data; console.log(myData); // prints "my custom data"

In the second example, an object is placed into String format: data: "{ 'dataKey': 'dataValue' }"

To parse this: const myData = JSON.parse(data);

Now it's an object, so you can access properties like: const dataVal = myData.dataKey; console.log(dataVal); // prints "dataValue"

dotNetkow avatar May 13 '20 15:05 dotNetkow

I believe my answer should explain things, but if not, please let us know. thanks.

dotNetkow avatar May 29 '20 19:05 dotNetkow

Just for the record: Maybe it is correct in some way, but sending string that wasn't JSON was causing errors and information couldn't make it through the bridge.

radarsu avatar May 29 '20 21:05 radarsu

It was possible to pass a string in the past, but was broken because of a cordova compatibility change, so iOS docs should be updated to match the android samples

Or it could be fixed to allow string data again, and probably allowing to pass a JSObject instead of a JSON formatted String.

jcesarmobile avatar Jun 01 '20 15:06 jcesarmobile

Hot Dog! You rock @radarsu. Ran into this exact same thing, and your suggestion fixed it. While I'm currently testing only on iOS, @dotNetkow maybe I'm doing it wrong, but the example of:

self.bridge.triggerWindowJSEvent(eventName: "myCustomEvent", data: "my custom data")

does not work. If I have this event listener:

    document.addEventListener('myCustomEvent', (info) => {
      console.log(JSON.stringify(info));
    });

Then it prints out:

{"isTrusted":false}

whereas when I trigger the event as:

self.bridge.triggerWindowJSEvent(eventName: "myCustomEvent", data: "{ 'dataKey': 'dataValue' }")

then I print out:

{"isTrusted":false,"dataKey":"dataValue"}

which is currently the only way I can receive data via this event handler.

alkrauss48 avatar Nov 10 '20 22:11 alkrauss48

I just wanted to also say the iOS docs for this cost me quite a bit of time as well. I kept trying trying to directly pass a string as the data value as the docs indicate.

self.bridge?.triggerWindowJSEvent(eventName: "dynamicLink", data: urlValue)

But when I follow the stringified object format as mentioned here and correctly shown in the Android docs it works as expected.

self.bridge?.triggerWindowJSEvent(eventName: "dynamicLink", data: "{ 'url': '\(urlValue)' }")

This is not trivial to debug when following the docs. If possible, it'd be nice if that could get updated to save a headache for others.

mcfarljw avatar Feb 25 '22 01:02 mcfarljw

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Nov 10 '22 19:11 ionitron-bot[bot]