从本机代码调用Javascript函数(iOS) [英] Calling Javascript function from native code (iOS)
问题描述
这是我的故事板。我使用SWRevealViewController插件在cordova应用程序中的侧边菜单。添加了主视图控制器作为最后视图控制器(在nav-bar中有项目)的类引用。
This is my Story Board. I have used SWRevealViewController plugin for my side menu in cordova application. Added "Main View Controller" as class reference to the Last view controller(having item in nav-bar) .
在我的cordova视图上从左到右滑动将显示侧窗。点击我在
On swiping from left to right on my cordova view will reveal Sidemenu. On click I'm saving selected row value in
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// menuItems is an array using which I'm populating the sidemenu
NSString * str = [menuItems objectAtIndex:indexPath.row];
}
我只想将此值传递给javascript函数。
I just want to pass this value to the javascript function.
在index.html中定义我的javascript函数
Defined my javascript function in index.html
<script >
(function () {
window.execute = function () {
alert(" machi");
var el = document.getElementById('search');
var scope = angular.element(el).scope();
scope.$apply(function() {
scope.execute();
});
}
})();
</script>
calling this function using
NSString *jsCommand = [NSString stringWithFormat:@"execute()"];
[self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
推荐答案
尝试将您的本机代码更改为:
Try to change your native code to:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// menuItems is an array using which I'm populating the sidemenu
NSString * str = [menuItems objectAtIndex:indexPath.row];
NSString *jsCommand = [NSString stringWithFormat:@"execute('%@')", str];
[self.webView stringByEvaluatingJavaScriptFromString:jsCommand];
}
您的javascript函数声明:
And your javascript function declaration to:
window.execute = function (str) {...}
您将在 str 函数参数中找到传递的值。
You will find your passed value into str function parameter.
这篇关于从本机代码调用Javascript函数(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!