Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:44:34+00:00 2026-06-02T13:44:34+00:00

I’m trying to pass a function reference from JavaScript in a WebView, so my

  • 0

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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:44:36+00:00Added an answer on June 2, 2026 at 1:44 pm

    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 a WebView — I define the following script-accessible method:

    #import <WebKit/WebKit.h>
    #import <JavaScriptCore/JavaScriptCore.h>
    
    - (void) search:(NSString *)prefix withCallback:(WebScriptObject *)callback;
    

    … 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:

    - (void) search:(NSString *)prefix withCallback:(WebScriptObject *)callback
    {
      // Functions get passed in as WebScriptObjects, which give you access to the function as a JSObject
      JSObjectRef ref = [callback JSObject];
    
      // Through WebView, you can get to the JS globalContext
      JSContextRef ctx = [[view mainFrame] globalContext];
    
      // In my case, I have a JSON string I want to pass back into the page as a JavaScript object
      JSValueRef obj = JSValueMakeFromJSONString(ctx, JSStringCreateWithCFString((__bridge CFStringRef)responseString)); 
    
      // And here's where I call the callback and pass in the JS object
      JSObjectCallAsFunction(ctx, ref, NULL, 1, &obj, NULL);
    }
    

    This actually works asynchronously as well, through Objective-C blocks, but the gist is above. Hope it helps someone else!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.