小编Him*_*esh的帖子

如何在目标C中调用JavaScript函数

我是目标C的新程序员.我在Objective C中添加了一些html文件.在这个html文件中包含简单的javascript函数.我需要通过目标C代码调用该函数.我通过谷歌尝试了很多时间.但我找不到真正的方法来做到这一点.Bellow包含html文件:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function myFunction()
            {
                alert("Hello World!");
            }
            </script>
    </head>

    <body>
        <button onclick="myFunction()">Try it</button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我这样调用这个函数:是正确还是错误?如果错了,该怎么做.请帮忙.

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()"];
Run Code Online (Sandbox Code Playgroud)

uiwebview ios

24
推荐指数
3
解决办法
4万
查看次数

标签 统计

ios ×1

uiwebview ×1