简体   繁体   English

iOS上的Cordova inAppBrowser中的executeScript执行不一致

[英]Inconsistent execution of executeScript in Cordova inAppBrowser on iOS

I'm having some trouble with some inAppBrowser behavior in my cordova app. 我的cordova应用程序中的inAppBrowser行为有麻烦。 Here's the code: 这是代码:

var codePass = fooCode;
var executeScriptFunc = function(event) { 
  ref.executeScript({
    code: codePass
  }, function (value) {});
  ref.removeEventListener('loadstop', executeScriptFunc);
};

var ref = cordova.InAppBrowser.open(fooObject.link, "_blank", "location=yes,enableViewportScale=yes");
ref.addEventListener('loadstop', executeScriptFunc)

The strange thing here is that the code works perfectly every time when emulated. 奇怪的是,每次仿真时,代码都能完美运行。 It opens the browser and executes the script no problem. 它打开浏览器并执行脚本没问题。 But when I try it on my actual iPhone device, it doesn't always work. 但是,当我在实际的iPhone设备上试用时,它并不总是能正常工作。 The script executes maybe every other time. 该脚本可能每隔两次执行一次。 But it's never even that consistent. 但这从来都不是一致的。

Both the emulator and iPhone are using iOS 9.3.4. 模拟器和iPhone均使用iOS 9.3.4。 Any ideas? 有任何想法吗?

If the site inside the inAppBrowser happens to be served via HTTPS, the callback for executeScript() will not work if the site employs a Content-Security-Policy HTTP response header that does not contain the gap: or gap-iab: schemes for the default-src directive. 如果inAppBrowser内部的网站恰好是通过HTTPS提供服务,则如果该网站使用Content-Security-Policy HTTP响应标头(其中不包含gap:gap-iab:方案executeScript() ,则executeScript()的回调将不起作用。 default-src指令。 These are needed because execution of the callback function on iOS relies on an iframe that gets added to the page . 这些是必需的,因为在iOS上执行回调函数依赖于添加到页面的iframe

You can check whether this is the root cause for the problem by opening Safari's Web Inspector for the inAppBrowser—it has a separate Web Inspector instance, independent of the parent application that opened it—and look out for a corresponding error message in the console. 您可以通过打开inAppBrowser的Safari Web Inspector(它有一个单独的Web Inspector实例,独立于打开它的父应用程序)来检查是否是问题的根本原因,并在控制台中查找相应的错误消息。 Note that you should open the console before executeScript() is run, or otherwise you might not get the error message. 请注意,应在运行executeScript()之前打开控制台,否则可能不会收到错误消息。

Make sure also that you don't have other event handlers firing at the same time during your polling. 还请确保在轮询期间您没有其他事件处理程序同时触发。

I had multiple pollers all firing every second and that's when I ran into the issue. 我有多个投票者都每秒触发一次,这就是我遇到的问题。

After changing the polling time so they all fired at different times, the issue went away. 在更改了轮询时间后,他们都在不同的时间触发了,问题就消失了。

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

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