简体   繁体   English

如何从JavaScript调用Objective-C方法?

[英]How can I call Objective-C methods from JavaScript?

I want to display SQLite Data base content on UIWebView. 我想在UIWebView上显示SQLite数据库内容。 For that i have a local HTML file. 为此,我有一个本地HTML文件。 On Body load in html file calling JavaScript Function. 在html文件中调用JavaScript函数的Body load。 Inside the Java Script how to call Objective-c method. 在Java Script内部如何调用Objective-c方法。 how to get Data from Objective c to Java Srcipt. 如何从Objective c获取数据到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 ...... Objective-C代码......

- (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. 在函数myJSFunc ()里面,如何调用-(NSArray*)FetchingDataFromDataBase.

The blog post given by graver explains it how to do it. 由graver提供的博客文章解释了如何做到这一点。

- (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. 仅当此方法返回YES时,才会处理新的加载请求。 We use that mechanism to communicate back to the Objective-C functions. 我们使用该机制与Objective-C函数进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM