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 7681333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:19:06+00:00 2026-05-31T18:19:06+00:00

In iOS, how can I call an Objective-C method from Javascript in a UIWebView

  • 0

In iOS, how can I call an Objective-C method from Javascript in a UIWebView and have it send data back to the Javascript? I know that this could be done on OS X using the Webkit library, but is this possible on iOS? How does PhoneGap achieve this?

  • 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-05-31T18:19:07+00:00Added an answer on May 31, 2026 at 6:19 pm

    There is an API to call JavaScript directly from Objective-C, but you cannot call Objective-C directly from Javascript.

    How to tell your Objective-C code to do something from the Javascript in your WebView

    You have to serialize your Javascript action into a special URL and intercept that URL in the UIWebView’s delegate’s shouldStartLoadWithRequest method.

    - (BOOL)webView:(UIWebView *)webView
            shouldStartLoadWithRequest:(NSURLRequest *)request
                        navigationType:(UIWebViewNavigationType)navigationType;
    

    There you can deserialize that special URL and interpret it to do what you want on the Objective-C side. (You should return NO in the above shouldStartLoadWithRequest method so the UIWebView doesn’t use your bogus URL to actually make an HTTP request to load a webpage.)

    How to Run Javascript Code from Objective-C

    Then you can run Javascript from Objective-C by calling this method on your webview.

    - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
    

    Example Code

    I recommend using a bogus URL scheme so it will be easy to tell the difference between your action URLs and legit requests. You can make this request in the Javascript along these lines:

    // JavaScript to send an action to your Objective-C code
    var myAppName = 'myfakeappname';
    var myActionType = 'myJavascriptActionType';
    var myActionParameters = {}; // put extra info into a dict if you need it
    
    // (separating the actionType from parameters makes it easier to parse in ObjC.)
    var jsonString = (JSON.stringify(myActionParameters));
    var escapedJsonParameters = escape(jsonString);
    var url = myAppName + '://' + myActionType + "#" + escapedJsonParameters;
    document.location.href = url;
    

    Then in the UIWebView.delegate‘s shouldStartLoadWithRequest method, you can inspect the URL scheme and fragment to check if it’s a normal request or one of your special actions. (The fragment of a URL is what comes after the #.)

    - (BOOL)webView:(UIWebView *)webView
            shouldStartLoadWithRequest:(NSURLRequest *)request
                        navigationType:(UIWebViewNavigationType)navigationType {
    
        // these need to match the values defined in your JavaScript
        NSString *myAppScheme = @"myfakeappname";
        NSString *myActionType = @"myJavascriptActionType";
    
        // ignore legit webview requests so they load normally
        if (![request.URL.scheme isEqualToString:myAppScheme]) {
            return YES;
        }
    
        // get the action from the path
        NSString *actionType = request.URL.host;
        // deserialize the request JSON
        NSString *jsonDictString = [request.URL.fragment stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    
        // look at the actionType and do whatever you want here
        if ([actionType isEqualToString:myActionType]) {
            // do something in response to your javascript action
            // if you used an action parameters dict, deserialize and inspect it here
        }
    
    
        // make sure to return NO so that your webview doesn't try to load your made-up URL
        return NO;
    }
    

    (Read this answer if you need help deserializing your json string into an NSDictionary.)

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

Sidebar

Related Questions

On iOS, I know that we can evaluate a trust with SecTrustEvaluate(). To create
On iOS there is this nice feature that an app developer can specify device-specific
As we all know tracking the position of a moving iOS device can be
I am making an AIR for iOS app that can download pdfs. I do
How can I rewrite Apple's GLCameraRipple example so that it doesn't require iOS 5.0?
I believe that UIView objects can delegate control to views in iOS programming. However,
When I call a certain function from my iOS app, it returns an id
First, I am super-new to Objective-C/iOS development and, in fact, this question is for
I am using the Google Geocoding API v3[1] from an iOS application but can't
A call of help from iOS developers out there. I am trying to recognize

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.