栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用UIWebView从HTML代码中调用目标C代码中的方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用UIWebView从HTML代码中调用目标C代码中的方法

在JS中调用Objective-C的方法:

下面的网址有助于做到这一点

无法执行,我们通过导航到其他页面来解决,在导航期间,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>

Objective-C代码

当您单击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 ];}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号