简体   繁体   English

可以在JavaScript中实现iPhone的后端/共享功能吗?

[英]Ok to implement backend/shared functionality for iPhone in JavaScript?

Is it ok to use the Webkit Javascript engine to implement cross-platform, non-GUI backend functionality on the iPhone, iPad? 是否可以使用Webkit Javascript引擎在iPhone,iPad上实现跨平台的非GUI后端功能? In my case, I was interested in re-using Javascript code that I have that works on top of SQLite. 就我而言,我对重用在SQLite上可以使用的Javascript代码感兴趣。 I thought I would need to re-implement the logic in Obj-C but perhaps I could just share it and expose some hooks into Obj-C using JSCocoa or straight through JavaScript core. 我以为我需要在Obj-C中重新实现逻辑,但是也许我可以共享它,并使用JSCocoa或直接通过JavaScript核心将一些挂钩暴露给Obj-C。 If I do this, is it ok to bypass the UIWebView control and go straight to JavaScriptCore or is that still considered a private framework? 如果执行此操作,是否可以绕过UIWebView控件并直接转到JavaScriptCore还是仍被视为私有框架? I am still searching and will update this if I find the answer. 我仍在搜索,如果找到答案,将对其进行更新。

Although the iPhone uses WebKit, UIWebView does not expose it as a public interface. 尽管iPhone使用WebKit,但UIWebView并未将其公开为公共接口。 To strictly follow apple guidelines, the only communication between a web page and the host application is by calling eval with stringByEvaluatingJavaScriptFromString and by processing resource requests. 为了严格遵循Apple准则,网页和主机应用程序之间的唯一通信是通过使用stringByEvaluatingJavaScriptFromString调用eval并处理资源请求。

You can do a lot in javascript. 您可以在javascript中做很多事情。 In fact, when it comes to controlling a UIWebView, javascript can often do more than Objective-C. 实际上,在控制UIWebView时,JavaScript通常可以完成比Objective-C更多的事情。 If your communications with the host application are simple enough you should be able to keep your existing code with minimal changes. 如果与主机应用程序的通信非常简单,那么您应该能够以最小的更改保留现有代码。

I'm pretty sure it is legal, in fact I have done this recently for a project at my company. 我非常确定这是合法的,实际上我最近在我公司的一个项目中做到了这一点。 We had a GWT app which spat out a JavaScript/HTML application which we needed to be port to iPhone and Android. 我们有一个GWT应用程序,它提供了一个JavaScript / HTML应用程序,需要将其移植到iPhone和Android。 Rather than doing a complete rewrite on each platform, we went down the (slightly insane) route of keeping the Java/JavaScript backend and sticking on native code to handle the UI. 我们没有在每个平台上进行完整的重写,而是沿途保留了Java / JavaScript后端并坚持使用本机代码来处理UI的(有点疯狂)路线。 Works well and you can't tell that in the background it is a browser that is running the show. 效果很好,您不能说在后台运行的是浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM