iOS JavaScript桥梁 [英] iOS JavaScript bridge

查看:69
本文介绍了iOS JavaScript桥梁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 从Objective-C执行JS方法
  2. 执行Objective-C方法JS

  3. 从Objective-C监听本地JS事件(例如DOM就绪事件)


解决方案

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





-



但是,我觉得这很简单,你可以自己尝试一下。当我需要这样做时,我亲自做了这件事。您也可以创建一个适合您需求的简单库。

1。从Objective-C执行JS方法



这只是一行代码。

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

关于官方的更多详情 UIWebView文档

2。从JS <​​/ h3>执行Objective-C方法

不幸的是稍微复杂一些,因为Mac OSX上存在的windowScriptObject属性(和类)不同,它允许完整的通信介于两者之间。



但是,您可以通过javascript自定义网址轻松拨打电话,例如:

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

从Objective-C中截取它:

   - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest :(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL * URL = [request URL];
if([[URL scheme] isEqualToString:@yourscheme]){
//解析URL对象的其余部分并执行函数
}
}

这不像应该的那样干净(或者使用windowScriptObject),但是它可以工作。



3。从Objective-C收听本机JS事件(例如DOM ready事件)



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



同样,不应该干净,但它确实有效。


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. Execute JS methods from Objective-C
  2. Execute Objective-C methods from JS
  3. Listen to native JS events from Objective-C (for example DOM ready event)

解决方案

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

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.

1. Execute JS methods from Objective-C

This is really just one line of code.

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

More details on the official UIWebView Documentation.

2. Execute Objective-C methods from JS

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.

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

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

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
   } 
}

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

3. Listen to native JS events from Objective-C (for example DOM ready event)

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桥梁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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