iOS JavaScript 桥接器

iOS JavaScript bridge(iOS JavaScript 桥接器)

本文介绍了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 桥接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS JavaScript 桥接器

基础教程推荐