I am working on passing data from swift to javascript inside a WKWebView
I have a custom class:
class AllInfo: AnyObject {
var title = "Special Title"
var description = "Special Description"
}
and initialize it with
var info = AllInfo()
I then have a WKWebView with which i pass a WKUserScript with which I have a source property of:
source: "changeDisplay('\(info)')"
My problem is how do I access this object in the javascript. I have attempted to access it like a javascript object as well as associative array with no luck. Here's the js funciton:
function changeDisplay(passedInfo) {
document.querySelector('h1').innerHTML = passedInfo.title
document.querySelector('h2').innerHTML = passedInfo.description
}
setTimeout(function () {changeDisplay();}, 5000);
EDIT: When I do attempt to access the object like this, I get undefined.
So my questions are:
Can I pass an AnyObject to JavaScript and access it? If not, what type should I make the swift class so that I can easily pass it.
I am tempted to just create a javascript object in swift as a string and pass that, but I feel there's a better way.
Thanks
EDIT: I answered how I was able to pass data as JSON below.