【发布时间】:2013-01-15 08:50:59
【问题描述】:
我是 Objective C 的新程序员。我在 Objective C 中添加了一些 html 文件。在这个 html 文件中包含简单的 javascript 函数。我需要通过目标 C 代码调用该函数。我用谷歌尝试了很多时间。但我找不到真正的方法来做到这一点。 波纹管包含 html 文件:
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("Hello World!");
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
</body>
</html>
我这样调用这个函数:它是正确的还是错误的??如果错了,该怎么做。请帮忙。
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"first" ofType:@"html"];
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
【问题讨论】:
-
警报是否显示?
-
当我点击按钮时,它会显示出来。但我想以编程方式调用该函数。
-
你想达到什么目的?您的应用程序是 HTML5 类型的吗?为什么不使用原生 UIKit?
-
先试试
[webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];,确定不是myFunction()的问题 -
我的目的是通过objective c将参数传递给html(javascript)。我想像报告一样显示 html 页面。所以我想用参数调用javascript函数。所以我尝试了这样的简单代码。我没有使用原生 UIKit。 @pbibergal 谢谢