<tfoot id='uAUIj'></tfoot>
      <bdo id='uAUIj'></bdo><ul id='uAUIj'></ul>
    <legend id='uAUIj'><style id='uAUIj'><dir id='uAUIj'><q id='uAUIj'></q></dir></style></legend>

      1. <small id='uAUIj'></small><noframes id='uAUIj'>

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

        iOS JavaScript 桥接器

        时间:2023-10-23
        • <legend id='gCvHF'><style id='gCvHF'><dir id='gCvHF'><q id='gCvHF'></q></dir></style></legend>
              <tbody id='gCvHF'></tbody>

          • <tfoot id='gCvHF'></tfoot>
            1. <small id='gCvHF'></small><noframes id='gCvHF'>

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

                  <bdo id='gCvHF'></bdo><ul id='gCvHF'></ul>
                  本文介绍了iOS JavaScript 桥接器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个应用程序,我将同时使用 UIWebView 中的 HTML5 和原生 iOS 框架.我知道我可以实现 JavaScript 和 Objective-C 之间的通信.是否有任何库可以简化这种通信的实现?我知道有几个库可以在 HTML5 和 javascript 中创建原生 iOS 应用程序(例如 AppMobi、PhoneGap),但我不确定是否有一个库可以帮助创建大量使用 JavaScript 的原生 iOS 应用程序.我需要:

                  I'm working on an app where I'm going to use both HTML5 in UIWebView and native iOS framework together. I know that I can implement communication between JavaScript and Objective-C. Are there any libraries that simplify implementing this communication? I know that there are several libraries to create native iOS apps in HTML5 and javascript (for example AppMobi, PhoneGap), but I'm not sure if there is a library to help create native iOS apps with heavy JavaScript usage. I need to:

                  1. 从 Objective-C 执行 JS 方法
                  2. 从 JS 执行 Objective-C 方法
                  3. 从 Objective-C 监听原生 JS 事件(例如 DOM 就绪事件)

                  推荐答案

                  有几个库,但我没有在大项目中使用过,所以你可能想尝试一下:

                  There are a few libraries, but I didn't used any of these in big projects, so you might want to try them out:

                  • WebViewJavascriptBridge:https://github.com/marcuswestin/WebViewJavascriptBridge
                  • GAJavaScript:https://github.com/newyankeecodeshop/GAJavaScript

                  但是,我认为这很简单,您可以自己尝试一下.当我需要这样做时,我个人正是这样做的.您还可以创建一个适合您需要的简单库.

                  However, I think it's something simple enough that you might give it a try yourself. I personally did exactly this when I needed to do that. You might also create a simple library that suits your needs.

                  这实际上只是一行代码.

                  This is really just one line of code.

                  NSString *returnvalue = [webView stringByEvaluatingJavaScriptFromString:@"your javascript code string here"];
                  

                  有关官方UIWebView 文档的更多详细信息.

                  More details on the official UIWebView Documentation.

                  不幸的是,这稍微复杂一些,因为在 Mac OSX 上没有相同的 windowScriptObject 属性(和类)允许两者之间完全通信.

                  This is unfortunately slightly more complex, because there isn't the same windowScriptObject property (and class) that exists on Mac OSX allowing complete communication between the two.

                  但是,您可以轻松地从 javascript 自定义 URL 调用,例如:

                  However, you can easily call from javascript custom-made URLs, like:

                  window.location = yourscheme://callfunction/parameter1/parameter2?parameter3=value
                  

                  然后用这个从 Objective-C 中截取它:

                  And intercept it from Objective-C with this:

                  - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
                     NSURL *URL = [request URL]; 
                     if ([[URL scheme] isEqualToString:@"yourscheme"]) {
                         // parse the rest of the URL object and execute functions
                     } 
                  }
                  

                  这并不像应有的那样干净(或使用 windowScriptObject),但它可以工作.

                  This is not as clean as it should be (or by using windowScriptObject) but it works.

                  从上面的解释可以看出,如果你想这样做,你必须创建一些 JavaScript 代码,将它附加到你想要监控的事件并调用正确的 window.location 调用然后被拦截.

                  From the above explanation, you see that if you want to do that, you have to create some JavaScript code, attach it to the event you want to monitor and call the correct window.location call to be then intercepted.

                  再一次,虽然不是应有的干净,但它确实有效.

                  Again, not clean as it should be, but it works.

                  这篇关于iOS JavaScript 桥接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:UIWebView:当应用程序进入后台时,iOS 6 中的 HTML5 音频暂停 下一篇:ITMS-90809:已弃用的 API 使用——Apple 将停止接受使用 UIWebView API 的应用提交

                  相关文章

                • <tfoot id='xZsxi'></tfoot>

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

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

                    <legend id='xZsxi'><style id='xZsxi'><dir id='xZsxi'><q id='xZsxi'></q></dir></style></legend>