xcode Cordova 插件只能在 iOS 中第二次打开时出现线程警告。你如何让插件初始化onload?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23662121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:55:46  来源:igfitidea点击:

Cordova plugins only work in iOS the second time its opened with a THREAD WARNING. How do you get plugins to initialize onload?

iosxcodecordovapluginsinappbrowser

提问by tbondt

Cordova Plugins in my iOS application only work after you open the app, close it by going back to the home screen, then reopening the app. Then a THREAD WARNING displays in the XCode log.

我的 iOS 应用程序中的 Cordova 插件仅在您打开应用程序后才起作用,返回主屏幕将其关闭,然后重新打开该应用程序。然后在 XCode 日志中显示线程警告。

2014-05-14 14:00:38.062 TLEMobile[28819:60b] THREAD WARNING: ['InAppBrowser'] took '192.306885' ms. Plugin should use a background thread.
2014-05-14 14:01:18.919 TLEMobile[28819:60b] THREAD WARNING: ['Notification'] took '39.698975' ms. Plugin should use a background thread.

What is causing this? And if I really need to set all the plugins as background threads how do you set that?

这是什么原因造成的?如果我真的需要将所有插件设置为后台线程,你如何设置?

I have added this param to my config.xml for ios to force the plugin to load on open but that doesn't seem to work.

我已将此参数添加到我的 config.xml for ios 以强制插件在打开时加载,但这似乎不起作用。

<param name="onload" value="true" />

I simply do not get it. I have tried reinstalling the platform as a whole. Individually removing all the plugins and putting them back and I am still getting the same issue. Plugins will only work when the app gets backgrounded and then brought back up. Here is the code from the recent build to ios as I start the app. It appears that the plugins load as th is starts but then are not accessible.

我只是不明白。我已经尝试重新安装整个平台。单独删除所有插件并将它们放回去,我仍然遇到同样的问题。插件仅在应用程序进入后台然后重新启动时才起作用。这是我启动应用程序时从最近构建到 ios 的代码。似乎插件在启动时加载,但随后无法访问。

2014-05-22 15:39:40.817 TLEMobile[5199:60b] Multi-tasking -> Device: YES, App: YES
2014-05-22 15:39:40.875 TLEMobile[5199:60b] Unlimited access to network resources
2014-05-22 15:39:41.131 TLEMobile[5199:60b] [CDVTimer][device] 0.559986ms
2014-05-22 15:39:41.136 TLEMobile[5199:60b] [CDVTimer][notification] 0.389040ms
2014-05-22 15:39:41.143 TLEMobile[5199:60b] [CDVTimer][inappbrowser] 3.835022ms
2014-05-22 15:39:41.149 TLEMobile[5199:60b] [CDVTimer][socialsharing] 1.318038ms
2014-05-22 15:39:41.153 TLEMobile[5199:60b] [CDVTimer][TotalPluginStartup] 23.202002ms
2014-05-22 15:39:41.659 TLEMobile[5199:60b] Resetting plugins due to page load.
2014-05-22 15:39:41.664 TLEMobile[5199:60b] IAB.close() called but it was already closed.
2014-05-22 15:39:45.843 TLEMobile[5199:60b] Finished load of: file:///var/mobile/Applications/220DD603-0644-4290-AE21-F9B6041D8408/TLEMobile.app/www/index.html#/tab/home
2014-05-22 15:39:54.914 TLEMobile[5199:60b] THREAD WARNING: ['Device'] took '14.032959' ms. Plugin should use a background thread.
2014-05-22 15:39:55.033 TLEMobile[5199:60b] THREAD WARNING: ['Notification'] took '100.118896' ms. Plugin should use a background thread.
2014-05-22 15:40:00.716 TLEMobile[5199:60b] THREAD WARNING: ['Notification'] took '5541.863037' ms. Plugin should use a background thread.

回答by Mgauk

I had same issue. Today I've found solution for my case.

我有同样的问题。今天我找到了我的案例的解决方案。

The problem was with js2native bridge. Cordova has different methods to communicate with native library (bridge mode):

问题出在 js2native 桥接器上。Cordova 有不同的方法与本机库进行通信(桥接模式):

  • XHR with, w/o or optional payload;
  • iframe hash with or w/o payload;
  • iframe nav.
  • 带、不带或可选有效载荷的 XHR;
  • 带或不带有效载荷的 iframe 哈希值;
  • iframe 导航

In cordova.js you can find iOSExec function. For iOS (except iOS 5) bridge mode is set to IFRAME_NAV.

在cordova.js 中你可以找到iOSExec 函数。对于 iOS(iOS 5 除外)桥接模式设置为 IFRAME_NAV。

Previous developer made couple of mistakes when on project design phase - and one of them is that whole content is removed from when view changes. And cordova's too. So cordova gets some error loops and plugins is not working.

以前的开发人员在项目设计阶段犯了几个错误 - 其中之一是在视图更改时删除了整个内容。科尔多瓦也是。因此,cordova 会出现一些错误循环,并且插件无法正常工作。

The solution is to force XHR bridge mode. I've inserted next code right in my deviceready callback:

解决办法是强制XHR桥接模式。我已经在我的设备就绪回调中插入了下一个代码:

cordova.exec.setJsToNativeBridgeMode(cordova.exec.jsToNativeModes.XHR_NO_PAYLOAD);

I hope this will help you. Good luck.

我希望这能帮到您。祝你好运。

回答by lastoneisbearfood

I had the same symptoms on cordova-ios 3.9.2 but my root cause was different from others listed here. Turns out it was a content security policy issue, gap://readyis required for the iframe cordova bridge.

我在cordova-ios 3.9.2上有相同的症状,但我的根本原因与此处列出的其他原因不同。原来这是一个内容安全策略问题,gap://ready是 iframe 科尔多瓦桥所必需的。

If none of the other solutions work for you, look at your content security policy and make sure to add gap:to the frame-src part of your <meta http-equiv="Content-Security-Policy"tag.

如果其他解决方案都不适合您,请查看您的内容安全策略并确保添加gap:<meta http-equiv="Content-Security-Policy"标签的 frame-src 部分。

Note: Xcode doesn't show any clue to fix this error but safari debugging shows the error very clearly.

注意:Xcode 没有显示任何修复此错误的线索,但 safari 调试非常清楚地显示了错误。