<i id='XI4sO'><tr id='XI4sO'><dt id='XI4sO'><q id='XI4sO'><span id='XI4sO'><b id='XI4sO'><form id='XI4sO'><ins id='XI4sO'></ins><ul id='XI4sO'></ul><sub id='XI4sO'></sub></form><legend id='XI4sO'></legend><bdo id='XI4sO'><pre id='XI4sO'><center id='XI4sO'></center></pre></bdo></b><th id='XI4sO'></th></span></q></dt></tr></i><div id='XI4sO'><tfoot id='XI4sO'></tfoot><dl id='XI4sO'><fieldset id='XI4sO'></fieldset></dl></div>

      <legend id='XI4sO'><style id='XI4sO'><dir id='XI4sO'><q id='XI4sO'></q></dir></style></legend>
    1. <tfoot id='XI4sO'></tfoot>
        <bdo id='XI4sO'></bdo><ul id='XI4sO'></ul>

      <small id='XI4sO'></small><noframes id='XI4sO'>

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

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

        <small id='BzGMP'></small><noframes id='BzGMP'>

      • <i id='BzGMP'><tr id='BzGMP'><dt id='BzGMP'><q id='BzGMP'><span id='BzGMP'><b id='BzGMP'><form id='BzGMP'><ins id='BzGMP'></ins><ul id='BzGMP'></ul><sub id='BzGMP'></sub></form><legend id='BzGMP'></legend><bdo id='BzGMP'><pre id='BzGMP'><center id='BzGMP'></center></pre></bdo></b><th id='BzGMP'></th></span></q></dt></tr></i><div id='BzGMP'><tfoot id='BzGMP'></tfoot><dl id='BzGMP'><fieldset id='BzGMP'></fieldset></dl></div>
      • <tfoot id='BzGMP'></tfoot>
          <legend id='BzGMP'><style id='BzGMP'><dir id='BzGMP'><q id='BzGMP'></q></dir></style></legend>
              <tbody id='BzGMP'></tbody>
              <bdo id='BzGMP'></bdo><ul id='BzGMP'></ul>

                本文介绍了使用 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 代码中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                EAAccessory/EASession teardown during background(EAAccessory/EASession 在后台拆除)
                Getting an NSArray of a single attribute from an NSArray(从 NSArray 获取单个属性的 NSArray)
                ImageIO: lt;ERRORgt; JPEG Corrupt JPEG data: premature end of data segment iphone - how to catch this?(ImageIO:lt;错误gt;JPEG 损坏的 JPEG 数据:iphone 数据段过早结束 - 如何捕捉到这个?)
                How to get indexPath.row of tableView which is currently being displayed?(如何获取当前正在显示的 tableView 的 indexPath.row?)
                Xcode iOS organizer submit to app store yields quot;The archive is invalidquot; error(Xcode iOS 管理器提交到应用商店产生“存档无效;错误)
                MFMessageComposeViewController alloc returns nil(MFMessageComposeViewController alloc 返回 nil)

                  <i id='76mtx'><tr id='76mtx'><dt id='76mtx'><q id='76mtx'><span id='76mtx'><b id='76mtx'><form id='76mtx'><ins id='76mtx'></ins><ul id='76mtx'></ul><sub id='76mtx'></sub></form><legend id='76mtx'></legend><bdo id='76mtx'><pre id='76mtx'><center id='76mtx'></center></pre></bdo></b><th id='76mtx'></th></span></q></dt></tr></i><div id='76mtx'><tfoot id='76mtx'></tfoot><dl id='76mtx'><fieldset id='76mtx'></fieldset></dl></div>
                  <legend id='76mtx'><style id='76mtx'><dir id='76mtx'><q id='76mtx'></q></dir></style></legend>
                • <small id='76mtx'></small><noframes id='76mtx'>

                  • <bdo id='76mtx'></bdo><ul id='76mtx'></ul>

                            <tbody id='76mtx'></tbody>
                          <tfoot id='76mtx'></tfoot>