我希望在UIWebView上显示SQLite
数据库的内容。为此,我有一个本地HTML文件。在HTML文件中Body加载时调用JavaScript函数。在JavaScript内部如何调用Objective-C方法?如何从Objective-C获取数据到Java Srcipt中?
<html>
<head>
<!-- a script inline -->
<script language="JavaScript" TYPE="text/javascript"></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
从数据库中获取数据。
script
元素上的language
属性已经被弃用了十多年,现在可以省略它。type
属性应该是小写的,但坦白地说,除非您包含一个不是用JavaScript编写的脚本(这是HTML5规范仅仅将现有实践编码化而不是指定新内容的领域之一),否则您不需要它。 - T.J. Crowder