使用UIWebView从HTML代码调用目标c代码中的方法 [英] Invoke method in objective c code from HTML code using UIWebView

查看:139
本文介绍了使用UIWebView从HTML代码调用目标c代码中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用中有.html文件。 HTML文件使用onClick方法的div块很少。
当我点击这些块时,我在Web视图中调用了一些javascript代码,但我还需要在源代码中了解这些事件。

I have .html file in my iOS app. HTML file has few div blocks with onClick methods. When I tap on these blocks I invoke some javascript code in web view, but I need also know about these events in my source code.

例如当我点击web元素并调用onClick我需要在代码中调用一些方法,例如 - (void)didTouchedWebElementWithId

For example when I tap on web element and onClick is called I need to invoke some method in the code e.g. - (void)didTouchedWebElementWithId

我可以这样做吗?谢谢。

Can I do this stuff. Thanks.

推荐答案

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

To call method of Objective-C in JS:

以下网址有助于做到这一点

the below url helps in doing that

如何从Javascript调用Objective C方法并将数据发送回iOS中的Javascript?

无法执行,我们通过导航到其他页面来解决方法,在导航过程中,webview委托将监视导航的前缀并执行我们指定的方法。

There is no way of executing, we make workaround by a navigation to other page and during the navigation, a webview delegate will watch for prefix of the navigation and execute the method we specified.

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并且相应的方法被调用

when you click button in html page the below delegate will fired and navigation cancelled because we return NO and respective method is called

- (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 ];
}

这篇关于使用UIWebView从HTML代码调用目标c代码中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆