iOS:在UIWebview中使用javascript调用obj-c方法 [英] iOS: call obj-c methods using javascript in a UIWebview

查看:116
本文介绍了iOS:在UIWebview中使用javascript调用obj-c方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个与JS网页协作的函数。我使用UIWebView来包含网页,当我希望网页与我的应用程序通信时,情况变得复杂。

I am writing a function that collaborates with a JS web page. I use UIWebView to contain the webpage and then situation has become complicated when I want the web page to communicate with my app.

使用 - stringByEvaluatingJavaScriptFromString:方法

但是有没有更简单的方法在网页中使用javascript调用obj-c函数?我尝试使用UIWebView委托方法,但我认为这太苛刻了。

But is there any easier way to call an obj-c function in the web page, using javascript? I tried using the UIWebView delegate method, but I think it's too hacky.

有什么建议吗?

推荐答案

我想使用委托是您可以在iOS WebView中使用的唯一(一个或两个)方法。但有几个包装可以帮助你轻松。

I guess using delegate is the only (one or two) methodology you can use in iOS WebView. But there are several wrappers that may help you easy out.


  1. EasyJSWebView - 这复制了Android中的开发体验。在Android中,您可以简单地使用WebView中的addJavascriptInterface()方法将Javascript桥接到Java。 EasyJSWebView提供同步样式和异步样式,用于从Objective-C方法获取返回值。

  1. EasyJSWebView - This replicates the development experience as in Android. In Android, you can simply use the addJavascriptInterface() method in WebView to bridge the Javascript to Java. EasyJSWebView provides both sync-style and async-style for getting the return value from Objective-C methods.

WebViewJavascriptBridge - 代码看起来有点像套接字编程。您可以在Objective-C中的服务器和Javascript中的客户端之间来回传递数据。

WebViewJavascriptBridge - The code may look a little bit like socket programming. You can pass data to and fro between the "server" in Objective-C and the "client" in Javascript.

GAJavaScript - 这可能会提供更好的DOM操作体验。

GAJavaScript - This may provide a better DOM manipulation experience.

看看所有这些并选择一个符合您需求的产品。

Take a look at all of them and choose one that fits your need.

这篇关于iOS:在UIWebview中使用javascript调用obj-c方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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