使用UIWebView从HTML代码中调用objective - c代码中的方法
Invoke method in objective c code from HTML code using UIWebView
我有。HTML文件在我的iOS应用程序。HTML文件有几个div块与onClick方法。当我点击这些块,我调用一些javascript代码在web视图,但我也需要知道这些事件在我的源代码。
例如,当我点击web元素和onClick被调用时,我需要在代码中调用一些方法,例如- (void)didTouchedWebElementWithId
我能做这个吗?谢谢。
在JS中调用Objective-C的方法:
下面的url有助于做到这一点
如何调用Objective C方法从Javascript和发送数据回Javascript在iOS?
没有办法执行,我们通过导航到其他页面来解决这个问题,在导航期间,webview委托将监视导航的前缀并执行我们指定的方法。
sample . html<html>
<head>
<script type='text/javascript'>
function getText()
{
return document.getElementById('txtinput').value;
}
function locationChange()
{
window.location = 'ios:webToNativeCall';
}
</script>
</head>
<body style='overflow-x:hidden;overflow-y:hidden;'>
<h2 style = "font-size:16px;" align='center'>Hi Welcome to Webpage</h2>
<br/>
<p align='center' style = "font-size:12px;">Please Click the button after entering the text</p>
<br/>
<center>
<input type='text' style='width:90px;height:30px;' id='txtinput'/>
</center>
<br/>
<center>
<input type='button' style='width:90px;height:30px;' onclick='locationChange()' value='click me'>
</center>
</body>
</html>
<<p> objective - c代码/strong> 当你点击html页面中的按钮时,下面的委托将被触发,导航将被取消,因为我们返回NO并且各自的方法被调用
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:@"ios:"]) {
// Call the given selector
[self performSelector:@selector(webToNativeCall)];
// Cancel the location change
return NO;
}
return YES;
}
- (void)webToNativeCall
{
NSString *returnvalue = [self.webviewForHtml stringByEvaluatingJavaScriptFromString:@"getText()"];
self.valueFromBrowser.text = [NSString stringWithFormat:@"From browser : %@", returnvalue ];
}
相关文章:
- 有没有一种方法可以通过只引用JavaScript来执行代码
- 从window.onbeforeunload调用方法背后的代码
- 是否有一种方法可以使用公共代码库在Java和JavaScript中进行输入验证
- 使用较少代码隐藏和显示选择菜单内容的更好方法是什么?javascript
- 我需要把createUser代码放在流星方法中吗
- 在 Javascript 中实现解耦代码/回调的正确方法是什么?
- 如何在jQuery.CSS()方法中为所有浏览器编写CSS代码
- 角度方法返回未绘制的代码
- 使用ScriptManager.RegisterStartupScript从代码隐藏调用javascript方法
- 崇高的jslint:任何忽略特定代码段的方法
- 有没有更好的方法来编写这个Show/hide JQuery代码
- 在我的代码中管理大量硬编码数据的最佳方法
- 需要减少用于获取日期格式的javascript方法代码
- 通过找到一种删除许多 if 语句的方法来简化代码
- 使用each()方法来动画化代码类型
- 我可以替换“;获取“;方法用“;POST”;代码库中的任何位置
- AngularJS控制器之间共享代码/方法/功能
- 如何确定字符串是否为代码方法
- 使用razor在javascript中调用带有参数的c#代码方法
- 如何从javascript调用服务器端类代码方法