How can I call Objective-C methods from JavaScript?

I want to display the SQLitecontents of a database in a UIWebView. For this, I have a local HTML file. Loading the body in an html file that calls the JavaScript function. Inside Java Script, how to call the Objective-c method. how to get data from Objective-c in Java Srcipt.

<html>
    <head>
        <!-- a script inline --> 
        <script language="JavaScript" TYPE="text/javascript">
            <!--
            function myJsFunc() 
            { 

                //here i want to call objective-c methods.

            } 
            // -->
            </script>
        </head>
    <Body Bgcolor=#800000 onload="myJsFunc(); return true;">

        </Body>
</html>

Objective-C code ......

- (void)viewDidLoad
{
        [super viewDidLoad];
        NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"Demo" ofType:@"html"];

    // make a file: URL out of the path
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];
    [myWebView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
    [self.view addSubView:myWebView];
}
-(NSArray*)FetchingDataFromDataBase{

    // Code for retriving data from SQlite data Base, and return NSArray;
    return myArray;
}

Inside the function myJSFunc(), you can call-(NSArray*)FetchingDataFromDataBase.

+3
source share
1 answer

A more serious blog post explains how to do this.

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

, . , . Objective-C.

+1

All Articles