IOS调用javascript通知功能

时间:2016-03-09 05:07:04

标签: javascript ios objective-c

我的应用就像iBook。我正在将html文件加载到webview中。

我将一个html文件加载到webview ..

 <img class="play_icon" src="images/play_icon.png" onclick="javascript:window.external.notify('Home pregnancy test.mp4')" />

如何从目标c

调用javascript函数window.external.notify()

如何获取此通知并获取目标c中的视频网址

1 个答案:

答案 0 :(得分:0)

请看下面的解决方案

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

    <body>
        <button onclick="yourFunctionName()">Try it</button>
    </body>
</html>

你的Obj-C代码

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:@"yourFunctionName()"];
 [webView stringByEvaluatingJavaScriptFromString:jsCallBack];

[webView stringByEvaluatingJavaScriptFromString:@"yourFunctionName()"];

您需要以下列方式实现执行,因为在一个地方实现上述Obj-C将不起作用。所以你必须按照以下方式去做。原因是工作将采用异步方式,这就是为什么

在UIWebViewDelegate中调用以下函数

[webView stringByEvaluatingJavaScriptFromString:@"yourFunctionName()"];

回调方法应该是-webViewDidFinishLoad:

如果您需要更多信息,请查看以下链接

Using JavaScript From Objective-C

希望这有帮助