无法使用JavaScriptCore和JSContext从UIWebView调用本机所有iOS函数

时间:2023-01-23 22:48:21

Let me explain my question it is bugging me from long time. Any help will be appreciated. Please help.

让我解释一下我的问题,它困扰了我很长时间。如有任何帮助,我们将不胜感激。请帮助。

I am working on hybrid kind of app in iOS. I am using UIWebView for loading webpages from http://myserver.com (its example its not actual URL) I have hosted multiple webpages on this server.

我在iOS中做混合式的应用。我正在使用UIWebView从http://myserver.com(它的示例不是实际的URL)加载网页。

I am using "JSContext" to communicate between javascript (webpages loaded from my server) in UIWebView and native iOS functions.

我正在使用“JSContext”在UIWebView中的javascript(从我的服务器加载的网页)和本地iOS函数之间进行通信。

I have followed procedure given at UIWebView JavaScript losing reference to iOS JSContext namespace (object) the context was getting lost but I have added workaround provided in solution of this question. Event then for some frame load inside UIWebView my iOS context is getting vanished. After following following steps context gets vanished.

我遵循了UIWebView JavaScript给出的过程失去了对iOS JSContext命名空间(object)的引用上下文丢失了,但是我增加了解决这个问题的方法。事件然后在UIWebView内的一些帧载入我的iOS环境消失了。执行以下步骤后,上下文将消失。

  1. I load page1 from myserver in UIWebView. Almost all works well all functions OS native iOS are called from javascript.
  2. 我在UIWebView中从myserver加载page1。几乎所有的函数都运行得很好所有OS本机iOS的函数都是从javascript调用的。
  3. Now when second page2 is loaded as result of interaction in page1, in same UIWebView javascript context gets vanished.
  4. 现在,当page1中的交互加载第二个page2时,在相同的UIWebView javascript上下文中会消失。

There is one more magical thing. if I have directly load page2 in UIWebView instead of indirect loading (page1->page2). All works well all native methods are called from UIwebView as expected. Please help. If you want more info please ask.

还有一件更神奇的事。如果我在UIWebView中直接加载page2而不是间接加载(page1->page2)。所有方法都运行良好,所有本机方法都按预期从UIwebView调用。请帮助。如果你想要更多的信息,请询问。

1 个解决方案

#1


2  

First of all, stealing and using the UIWebView's JavaScriptContext in this way is likely a path to app rejection.

首先,以这种方式窃取并使用UIWebView的JavaScriptContext很可能是应用拒绝的路径。

But if this is just a science project...

但如果这只是一个科学项目……

You might try another technique for obtaining the JavaScriptContext. I present a solution in this GitHub project:

您可以尝试另一种获取JavaScriptContext的技术。在这个GitHub项目中,我提出了一个解决方案:

https://github.com/TomSwift/UIWebView-TS_JavaScriptContext

https://github.com/TomSwift/UIWebView-TS_JavaScriptContext

Basically I'm using some knowledge of how WebKit works to implement a category on NSObject to intercept some non-handeled WebKit delegate methods which are called whenever a new JavaScriptContext is created for a frame.

基本上,我正在使用WebKit如何工作的一些知识来实现NSObject上的一个类别,以拦截一些非手工WebKit委托方法,每当为一个框架创建一个新的JavaScriptContext时,这些方法就会被调用。

#1


2  

First of all, stealing and using the UIWebView's JavaScriptContext in this way is likely a path to app rejection.

首先,以这种方式窃取并使用UIWebView的JavaScriptContext很可能是应用拒绝的路径。

But if this is just a science project...

但如果这只是一个科学项目……

You might try another technique for obtaining the JavaScriptContext. I present a solution in this GitHub project:

您可以尝试另一种获取JavaScriptContext的技术。在这个GitHub项目中,我提出了一个解决方案:

https://github.com/TomSwift/UIWebView-TS_JavaScriptContext

https://github.com/TomSwift/UIWebView-TS_JavaScriptContext

Basically I'm using some knowledge of how WebKit works to implement a category on NSObject to intercept some non-handeled WebKit delegate methods which are called whenever a new JavaScriptContext is created for a frame.

基本上,我正在使用WebKit如何工作的一些知识来实现NSObject上的一个类别,以拦截一些非手工WebKit委托方法,每当为一个框架创建一个新的JavaScriptContext时,这些方法就会被调用。