I’m trying to pass a function reference from JavaScript in a WebView, so my Objective-C code can call then back into that function when the asynchronous Obj-C method completes. I see how to pass in simple types — strings, numbers, etc. — but can’t seem to figure out how to pass something a function reference, like
window.myRootObject.myObjCSelector("some-string", function(data) {
console.log(data);
});
I’ve Googled around, but continue to come up short, and haven’t found a reference to this in the docs (yet). Working in Cocoa, but would love to be able to do the same sort of thing on iOS.
Thanks in advance for any help!
Edit: The function argument should be anonymous — hopefully this clarifies. 🙂
Finally figured this out. There might be a better way (and if so, please chime in!) but the following seems to work. In my Obj-C (
NSObject-derived) class — into which I pass a reference to aWebView— I define the following script-accessible method:… which is intended to take two arguments: a string to search with, and an anonymous function callback to handle the result. It’s implemented thusly:
This actually works asynchronously as well, through Objective-C blocks, but the gist is above. Hope it helps someone else!