作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在 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/
我是一名优秀的程序员,十分优秀!