bug(docs): triggerWindowJSEvent doesn't allow string data
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.
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"
I believe my answer should explain things, but if not, please let us know. thanks.
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.
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.
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.
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.
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.