3

I want to display SQLite Data base content on UIWebView. For that i have a local HTML file. On Body load in html file calling JavaScript Function. Inside the Java Script how to call Objective-c method. how to get Data from Objective c to 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(), how can call -(NSArray*)FetchingDataFromDataBase.

2
  • 2
    Take a look at this blog post: blog.techno-barje.fr/post/2010/10/06/… Commented Apr 18, 2012 at 9:45
  • 1
    The language attribute on script elements has been deprecated for more than a decade, you can leave it off now. The type attribute should be in lower case, but frankly, you don't need it unless you're including a script not written in JavaScript (this is one of the areas where the HTML5 spec is just codifying existing practice, rather than dictating something new). Commented Apr 18, 2012 at 9:46

1 Answer 1

1

The blog post given by graver explains it how to do it.

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

If you have the above method in your class, this method will be called before a new page is loaded. Only if this method returns YES, the new load request is processed. We use that mechanism to communicate back to the Objective-C functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Can i call Objective c Fuction from External Java Script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.