本文介绍了访问UIWebView的JavaScriptCore引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在iOS7中发现了一个新的框架:JavaScriptCore。

I just discovered a new framework available in iOS7: JavaScriptCore.

它看起来很棒,但是如何访问 UIWebView ?

It looks awesome, but how can I access the runtime/context of a UIWebView?

推荐答案

这没有官方机制。但是我知道有两种方法可以获得 JSContext 。我可能不会在运送应用程序中使用。

There is no official mechanism for this. But there are two approaches I know about to get the JSContext. I probably wouldn't use either in a shipping app.


  1. 使用KVC访问内部 UIWebView 属性。在这篇博客文章中详细解释:

  1. Use KVC to access internal UIWebView properties. Explained in detail in this blog post: http://blog.impathic.com/post/64171814244/true-javascript-uiwebview-integration-in-ios7



JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];




  1. 添加魔术方法 NSObject 通过类别。这里详细解释:

  1. Add a magic method to NSObject via a category. Explained in more detail, here: https://github.com/TomSwift/UIWebView-TS_JavaScriptContext



@implementation NSObject (magic)
- (void) webView: (id) unused didCreateJavaScriptContext: (JSContext*) ctx forFrame: (id) frame
{
    // ...
}
@end

这篇关于访问UIWebView的JavaScriptCore引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:26