Help needed on objective-c/requirejs interop

I am hosting the webview controller in an OSX app. I have followed the code sample in calljs and i can successfully call javascript functions inside script tags from objective-c. This integration requires that the objective-c program know the name of the javascript function. for instance i have a javascript function called “objective_c_entry()”, and in my objective c wrapper, i can call that entry point using:

NSString *callResult = [[webView windowScriptObject] callWebScriptMethod:@"objective_c_entry" withArguments:args];

Now I want to move all my javascript into require.js modules; i am not clear how to reference a function in a require.js module from within objective-c. if i am moving my “objective_c_entry” function into a require.js module named “integration”, what string do I pass to callWebScriptMethod?

2 thoughts to “Help needed on objective-c/requirejs interop”

  1. The way you call a function defined in a script loaded using require.js is the same as one loaded using a script tag or declared inline. The only key difference is timing: that function won’t be available until the callback function you passed as the second argument in the call to require() is called (which means that the script specified in the first parameter and its dependencies are loaded).

    You could put something in the callback to let your Objective C code know that the functions are available (like navigating to an anchor, which would trigger a shouldStartLoadWithRequest of UIWebViewDelegate where you can set an internal flag and then cancel the navigation), but I’m not sure if the convenience of require.js is worth the hassle/overhead of dealing with the timing issue.

  2. thanks Mike. I am using webgl with quite a few shaders and for that reason i am motivated to go something like require that lets me get all the shaders out of the .htm file, along with all the js to manage the webgl. i ultimately decided to keep the very short script, which integrates with objective-c, in the base html file. and then i have it call into the require.js modules using a require() wrapper. i may find a timing issue which bites me but so far it seems to work reasonably well.

Comments are closed.