How to access JS function in CPWebView

38 views
Skip to first unread message

Todd Freese

unread,
Feb 10, 2015, 11:51:24 AM2/10/15
to objec...@googlegroups.com
How do I call into and get a return value for a JS function on a web page loaded inside a CPWebView?

I searched the archives and found a bunch of postings for going the other way (Access a capp object from the webpage). 

Todd

Todd Freese

unread,
Feb 10, 2015, 12:19:43 PM2/10/15
to objec...@googlegroups.com
I found this:

CPLog([[webView windowScriptObject] evaluateWebScript:"getMarkList()"]);

However, [webView windowScriptObject] is undefined.

This is called after the page has loaded in the CPWebView.

T

Ross Boucher

unread,
Feb 10, 2015, 12:45:32 PM2/10/15
to objec...@googlegroups.com
Is your page on the same origin? If not, you won't be able to communicate with it easily through a frame, you'd be better researching window.postMessage.

--
You received this message because you are subscribed to the Google Groups "Cappuccino & Objective-J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectivej+...@googlegroups.com.
To post to this group, send email to objec...@googlegroups.com.
Visit this group at http://groups.google.com/group/objectivej.
For more options, visit https://groups.google.com/d/optout.

CappCoder

unread,
Feb 11, 2015, 12:33:28 PM2/11/15
to objec...@googlegroups.com
I've done it.

Use [webView DOMWindow] to  access the window object. You can then call whatever function you want. You will run into cross origin issues however depending on your requirements. You will then have to use HTML5 postMessage APIs which don't work all that great in IE 9 . 

Todd Freese

unread,
Feb 11, 2015, 11:05:07 PM2/11/15
to objec...@googlegroups.com
Thanks of all the suggestions. I got it working great.

This post really helped me out:


Todd
Reply all
Reply to author
Forward
0 new messages