Invoke method in objective c code from HTML code using UIWebView

37,292

To call method of Objective-C in JS:

the below url helps in doing that

How to invoke Objective C method from Javascript and send back data to Javascript in iOS?

There is no way of executing, we make workaround by a navigation to other page and during the navigation, a webview delegate will watch for prefix of the navigation and execute the method we specified.

sample.html

<html>
    <head>
        <script type='text/javascript'>
            function getText()
            {
                return document.getElementById('txtinput').value;
            }
            function locationChange()
            {
                window.location = 'ios:webToNativeCall';
            }
        </script>
    </head>
    <body style='overflow-x:hidden;overflow-y:hidden;'>
        <h2 style = "font-size:16px;" align='center'>Hi Welcome to Webpage</h2>
        <br/>
        <p align='center' style = "font-size:12px;">Please Click the button after entering the text</p>
        <br/>
        <center>
            <input type='text' style='width:90px;height:30px;' id='txtinput'/>
        </center>
        <br/>
        <center>
            <input type='button' style='width:90px;height:30px;' onclick='locationChange()' value='click me'>
        </center>
    </body>
</html>

Objective-C code

when you click button in html page the below delegate will fired and navigation cancelled because we return NO and respective method is called

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] absoluteString] hasPrefix:@"ios:"]) {

        // Call the given selector
        [self performSelector:@selector(webToNativeCall)];        
        // Cancel the location change
        return NO;
    }
    return YES;
}

- (void)webToNativeCall
{
    NSString *returnvalue =  [self.webviewForHtml stringByEvaluatingJavaScriptFromString:@"getText()"];

    self.valueFromBrowser.text = [NSString stringWithFormat:@"From browser : %@", returnvalue ];
}
Share:
37,292
Matrosov Oleksandr
Author by

Matrosov Oleksandr

Updated on November 24, 2020

Comments

  • Matrosov Oleksandr
    Matrosov Oleksandr over 3 years

    I have .html file in my iOS app. HTML file has few div blocks with onClick methods. When I tap on these blocks I invoke some javascript code in web view, but I need also know about these events in my source code.

    For example when I tap on web element and onClick is called I need to invoke some method in the code e.g. - (void)didTouchedWebElementWithId

    Can I do this stuff. Thanks.

  • Gustav
    Gustav over 9 years
    Beware of the security implications!
  • Vinodh
    Vinodh over 9 years
    can you be more detail about it ?
  • Gustav
    Gustav over 9 years
    suppose you have a method called getSecretToken in your class. Then you could call it from Javascript
  • Soumya Ranjan
    Soumya Ranjan about 9 years
    Thanks Bro...Its really a awesome answer. I stucked from last 2 days. I already post my q but didn't get any valuable solution. Finally your answer solved my problem... So much thanks
  • Ankita Shah
    Ankita Shah over 8 years
    How can I use this for getting value from server to native method call?
  • Anjali Bhimani
    Anjali Bhimani almost 7 years
    how i can call method in objective c from JavaScript without Click event,like if my game is in javascript and it is over at that time i want to notify in my Objective c code then how i can call ? please help me.
  • Maria
    Maria over 5 years
    instead of using window.location, can i use ajax?