gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:41 27 4
gpt4 key购买 nike

我想在 UIWebView 上显示 SQLite 数据库内容。为此,我有一个本地 HTML 文件。在调用 JavaScript 函数的 html 文件中加载正文。在 Java Script 中如何调用 Objective-c 方法。如何从 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 代码……

- (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;
}

在函数 myJSFunc() 中,如何调用 -(NSArray*)FetchingDataFromDataBase.

最佳答案

graver 给出的博文解释了如何做。

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

如果您的类中有上述方法,则在加载新页面之前将调用此方法。只有当此方法返回 YES 时,新的加载请求才会被处理。我们使用该机制与 Objective-C 函数进行通信。

关于javascript - 如何从 JavaScript 调用 Objective-C 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10206793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com