在没有UIWebView的情况下运行JavaScript? [英] Run javascript without UIWebView possible?
问题描述
是否可以在不使用 UIWebView
的情况下运行一些javascript代码(也许将 NSString
作为输入)?我想在一个单独的线程中运行此代码,所以不可能使用UIWebView,并且我真的不想呈现任何内容,只需返回结果即可.
Is it possible to run some javascript code (having perhaps an NSString
as input) without using a UIWebView
? I would like to run this code in a separate thread so UIWebView is not possible and I don't really want to render anything, just get back the results.
推荐答案
我还没有尝试过,但这是一个主意:
I have not tried this, but here is one idea:
编写代码以分配一个空的 UIWebView
,但不会将其添加为任何子视图,而只需直接调用
Write code to allocate an empty UIWebView
but doesn't add it as a subview anywhere, and just directly call - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
on your javascript.
然后使用 NSOperation
并行运行它.
有什么原因可能对您不起作用?我认为这样可以避免为 UIWebView
显示任何内容.
Any reason that might not work for you? I think it would avoid displaying anything for the UIWebView
.
作为一个旁注,我想不出您可以在javascript中做的任何事情,而这些事情您不能直接在Objective-C中做得更快.除了解析您从外部获取的javascript之外,根据具体情况,Apple可能会认为这是不可以的.
As a side note, I can't think of anything you can do in javascript that you can't do more quickly directly in Objective-C. Except maybe parsing javascript that you get externally, which may be considered a no-no by Apple, depending on the details.
根据您的目标进行猜测:如果您想解析JSON服务器回复,我建议托管在Google代码上的json-framework库.它具有一个不错的界面,并且易于学习和集成.
Guessing at your goals: If you want to parse a JSON server reply, I recommend the json-framework library hosted on google code. It has a nice interface, and it is surprisingly easy to learn and integrate.
这篇关于在没有UIWebView的情况下运行JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!