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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15537320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:49:03  来源:igfitidea点击:

Invoke method in objective c code from HTML code using UIWebView

javascriptiosuiwebview

提问by Matrosov Alexander

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.

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

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

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

Can I do this stuff. Thanks.

我可以做这个东西吗。谢谢。

回答by Vinodh

To call method of Objective-C in JS:

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

the below url helps in doing that

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

How to invoke Objective C method from Javascript and send back data to Javascript in iOS?

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

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.

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

sample.html

示例.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 code

Objective-C 代码

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

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