简体   繁体   English

无法使用JavaScriptCore和JSContext从UIWebView调用本机所有iOS功能

[英]Unable to call native all iOS functions from UIWebView using JavaScriptCore and JSContext

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. 我正在开发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. 我正在使用UIWebViewhttp://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. 我已遵循UIWebView JavaScript上给出的过程, 失去了对上下文丢失的iOS JSContext名称空间(对象)引用,但是我添加了此问题的解决方案中提供的解决方法。 Event then for some frame load inside UIWebView my iOS context is getting vanished. 事件然后为UIWebView内的某些框架负载而消失,我的iOS上下文消失了。 After following following steps context gets vanished. 在执行以下步骤后,上下文消失了。

  1. I load page1 from myserver in UIWebView . 我从UIWebView myserver加载page1 Almost all works well all functions OS native iOS are called from javascript . javascript调用OS本机iOS的所有功能几乎都能正常运行。
  2. Now when second page2 is loaded as result of interaction in page1 , in same UIWebView javascript context gets vanished. 现在,由于page1中的交互作用而加载了第二个page2时,在同一UIWebView javascript上下文中将消失。

There is one more magical thing. 还有一件神奇的事情。 if I have directly load page2 in UIWebView instead of indirect loading ( page1 -> page2 ). 如果我已经在UIWebView直接加载page2而不是间接加载( page1- > page2 )。 All works well all native methods are called from UIwebView as expected. 一切正常,可以从UIwebView调用所有本机方法。 Please help. 请帮忙。 If you want more info please ask. 如果您想了解更多信息,请询问。

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. 您可以尝试另一种获取JavaScriptContext的技术。 I present a solution in this GitHub project: 我在这个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时,这些方法就会被调用。

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

相关问题 使用JavascriptCore和JSContext从javascript调用本机Swift代码 - Calling native Swift code from javascript using JavascriptCore and JSContext 是否会使用UIWebview中的JSContext来重新调整我的应用程序? - Will using the JSContext from a UIWebview get my app rejcted? 如何在ios5中获取UIwebView的JSContext? - How to get UIwebView's JSContext in ios5? IOS7中引入的带有Javascriptcore的UIWebview的ARC问题 - ARC issue of UIWebview with Javascriptcore introduced in IOS7 UIWebView JavaScript失去对iOS JSContext名称空间(对象)的引用 - UIWebView JavaScript losing reference to iOS JSContext namespace (object) 使用JavaScriptCore的JSContext时如何在JavaScript中定义不需要特定顺序的类 - How to define classes in JavaScript that don't require a specific order when using JSContext of JavaScriptCore 如何使用JavaScriptCore从C调用JavaScript函数? - How to call a JavaScript function from C using JavaScriptCore? 使用ViewControllers中iOS 7中的JavaScriptCore从javascript触发目标C方法 - Trigger objective C method from javascript using JavaScriptCore in iOS 7 in ViewControllers 使用Swift iOS中的Javascriptcore框架将NSobject传递给javascript - Passing NSobject to javascript using Javascriptcore framework from Swift iOS UIWebView上的JavaScriptCore崩溃 - JavaScriptCore crash on UIWebView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM