Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Passing a JavaScript callback to a WebKit plug-in
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing a JavaScript callback to a WebKit plug-in



I got the code to invoke a JavaScript callback from a WebKit plug-in working. It looks like this:

// 'container' is the object passed as 'WebPlugInContainerKey' to our plugInViewWithArguments: method
// 'callback' is a WebScriptObject reference, representing the JavaScript callback function
// 'result' is an NSString representing some data we want to pass to the callback
WebFrame *frame = [container webFrame];
JSGlobalContextRef jsCtx = [frame globalContext];
JSStringRef s = JSStringCreateWithCFString((CFStringRef) result);
JSValueRef jsArgs[1];
jsArgs[0] = JSValueMakeString(jsCtx, s);
JSObjectRef fn = [callback JSObject];
JSObjectCallAsFunction(jsCtx, fn, NULL, 1, jsArgs, NULL);
JSStringRelease(s);


I'm still a bit shaky about whether and when to retain or release objects (years of Java have atrophied my memory management sense a bit, I guess), but apart from probably leaking memory, the above code appears to work fine in Safari under Mac OS X 10.5.3. Next stop, iPhone...

(N.B. Regarding the JSObject method in WebScriptObject: you are correct that it is documented in the current documentation; I suspect I initially looked at an obsolete version, but after downloading the latest and greatest with Xcode's documentation browser, I found it. There does seem to be an error in the WebScriptObject documentation page, though: in the method overview, JSObject is missing, apparently replaced by InstanceMethod. When you click that link, you get to a paragraph with the heading InstanceMethod, but documenting the JSObject method.)

Thanks for your help!

- Thomas

Darin Adler wrote:
On Jun 17, 2008, at 11:47 AM, Thomas Okken wrote:

I have a method that I'm going to call from JavaScript:

- (NSString *) methodToBeCalledFromJavaScript: (NSString *) someArg callback: (WebScriptObject *) callback
{
JSObjectRef fn = [callback JSObject];
//...do lengthy task in a background thread...
//...rejoin foreground thread...
JSValueRef result = JSObjectCallAsFunction(ctx, fn, nil, argc, argv, nil);
//...clean up...
}


And I'll use it from JavaScript like this:

  function callback(foo, bar, baz)
  {
      //...display results in web page...
  }

someButton.onclick = "theObj.methodToBeCalledFromJavaScript("Hello!", callback);"

Does that look right?

Yes.

I'm a bit confused about the [callback JSObject] call -- I don't see a method named JSObject in the WebScriptObject reference.

It's new, so it might not be in an older reference.

Also, the JavaScriptCore reference mentions header files like <JavaScriptCore/JSBase.h> which I can't find on my system, even though I have the latest iPhone SDK installed. Is this a separate download?

You should have these latest JavaScriptCore headers on your computer if you're developing using the OS X 10.5 SDK or on 10.5 using the system headers.


    -- Darin

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webkitsdk-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Passing a JavaScript callback to a WebKit plug-in (From: Thomas Okken <email@hidden>)
 >Re: Passing a JavaScript callback to a WebKit plug-in (From: Darin Adler <email@hidden>)
 >Re: Passing a JavaScript callback to a WebKit plug-in (From: Thomas Okken <email@hidden>)
 >Re: Passing a JavaScript callback to a WebKit plug-in (From: Darin Adler <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.