简体   繁体   English

Cordova 3.3.1-0.1.2(Phonegap)插件不起作用

[英]Cordova 3.3.1-0.1.2 (Phonegap) plugins doesn't work

I have a cordova 3.3.1-0.1.2 test project for iOS, where I use the following plugins ( cordova plugin ls ): 我有一个适用于iOS的cordova 3.3.1-0.1.2测试项目,我使用以下插件( cordova plugin ls ):

[ 'com.phonegap.plugins.PushPlugin',
  'org.apache.cordova.console',
  'org.apache.cordova.device',
  'org.apache.cordova.splashscreen' ]

I've added the plugins like this cordova plugin add org.apache.cordova.device . 我添加了像这个cordova plugin add org.apache.cordova.device In my git repo, I see, that it adds a bunch of files to the plugins/org.apache.cordova.device directory, creates a CDVDevice.h and a CDVDevice.m in my iOS plugin directory, updates the ios.json , updates the *.xcodeproj file and adds the plugins to my config.xml inside platforms/ios/test-app/config.xml : 在我的git repo中,我看到它将一堆文件添加到plugins/org.apache.cordova.device目录,在我的iOS插件目录中创建一个CDVDevice.h和一个CDVDevice.m ,更新ios.json ,更新*.xcodeproj文件并添加插件到我的config.xmlplatforms/ios/test-app/config.xml

<feature name="Device">
    <param name="ios-package" value="CDVDevice" />
</feature>

But when I try to access the device or window.device property inside JS, it tells me, that device is undefined. 但是当我尝试访问JS中的devicewindow.device属性时,它告诉我,该device是未定义的。 The weird thing is, that the pushPlugin is present: 奇怪的是,pushPlugin存在:

document.addEventListener("deviceready", function () {
   console.log(device);
   console.log(window.device);
   console.log(window.plugins.pushNotification);
});

The window.plugins object only lists the pushNotification plugin as a property. window.plugins对象仅将pushNotification插件列为属性。 It's weird, because they are all installed, and during the installation, cordova said, everything was ok. 这很奇怪,因为它们都已安装完毕,在安装过程中,cordova说,一切都很好。

I'm a little bit confused, about the outdated, and mixed-with-phonegap documentation, but that would be ok, if one of them would work. 我有点困惑,关于过时的,混合的电话差距文档,但如果其中一个可行,那就没关系。 I also saw a plugin definition, inside a config.xml like this 我还在这样的config.xml看到了一个插件定义

<gap:plugin name="org.apache.cordova.device" />

Can someone explain me, what's the difference? 有人可以解释一下,有什么区别? Is the way (with feature ) that I'm working outdated with cordova 3.3? 我使用cordova 3.3工作的方式( feature )是什么? When I try to use the <gap:plugin... format, my app crashes on startup. 当我尝试使用<gap:plugin...格式时,我的应用程序在启动时崩溃。

So, please help me to fix this, and clear my mind ;) 所以,请帮助我解决这个问题,并清除我的想法;)

Unfortunately, none of the answers worked. 不幸的是,没有一个答案奏效。 Luckily, I found the solution in this mailing list: 幸运的是,我在这个邮件列表中找到了解决方案:

http://mail-archives.apache.org/mod_mbox/cordova-dev/201312.mbox/%3CCABiQX1Vat5XvmKkWt=+viL9oXWnOiAz5ee95h8oJp0j4MU9pJQ@mail.gmail.com%3E http://mail-archives.apache.org/mod_mbox/cordova-dev/201312.mbox/%3CCABiQX1Vat5XvmKkWt=+viL9oXWnOiAz5ee95h8oJp0j4MU9pJQ@mail.gmail.com%3E

There they say, that this is a bug in cordova 3.3.1. 他们说,这是cordova 3.3.1中的一个错误。 In this versions, the plugins get copied into the .staging directory of the different platforms. 在此版本中,插件将被复制到不同平台的.staging目录中。

So, downgrading with npm to cordova 3.3.0 now worked (on Mac OS X): 因此,使用npm降级到cordova 3.3.0现在已经有效了(在Mac OS X上):

sudo npm remove -g cordova
sudo npm view cordova versions
sudo npm install -g cordova@3.3.0-rc.1

And also check, if cordova is maybe installed from phonegap too. 如果可以从phonegap安装cordova,还要检查一下。 If you only use cordova, and non of the phonegap features (like remote build), you can safely remove the phonegap package with 如果你使用cordova,而不使用phonegap功能(如远程构建),你可以安全地删除phonegap包

sudo npm remove -g phonegap

Update 更新

The current version 3.4.0 works fine, and doesn't have this issues. 当前版本3.4.0工作正常,并没有这个问题。

plugin definition using gap:plugin is for configuring plugins for phonegap build (so you don't need it for local build). 使用gap:plugin插件定义gap:plugin用于配置phonegap构建的插件(因此您不需要它用于本地构建)。


As MBillau suggested, the plugins are added to the platform only after you run cordova prepare ios (or cordova build ios which launches prepare and then builds the project). 正如MBillau所建议的那样,只有在你运行cordova prepare ios (或cordova build ios启动准备然后构建项目)后才能将插件添加到平台。

When you run prepare, javascript files of the plugin should be copied to the www/plugins/pluginfullname/www folder of the platforms/ios folder . 运行prepare时,应将插件的javascript文件复制到platforms/ios folderwww/plugins/pluginfullname/www文件platforms/ios folder

If the files are not there it may be an issue with the ios.json file being corrupted. 如果文件不存在,则可能是ios.json文件损坏的问题。 In that case, sometimes uninstalling and then re-installing the plugin solves the problem. 在这种情况下,有时卸载然后重新安装插件可以解决问题。 In some case you have to remove the ios.json file, and empty the plugins folder at the root of your project (and maybe allso the platforms folder) and re-install all the plugins. 在某些情况下,您必须删除ios.json文件,并清空项目根目录下的plugins文件夹(也可能是平台文件夹)并重新安装所有插件。


If window.plugins object only lists the pushnotification plugin it is just because it is the only plugin installed in this object : 如果window.plugins对象只列出了pushnotification插件,那只是因为它是这个对象中唯一安装的插件:

  • org.apache.cordova.console is in console and cordova.logger org.apache.cordova.consoleconsolecordova.logger
  • org.apache.cordova.device is in window.device org.apache.cordova.devicewindow.device
  • org.apache.cordova.splashscreen is in navigator.splashscreen org.apache.cordova.splashscreennavigator.splashscreen

To know javascript object name to use, you have to read the plugin's documentation or look at the js-module/clobbers property in the plugin.xml file of the plugin. 要了解要使用的javascript对象名称,您必须阅读插件的文档或查看插件的plugin.xml文件中的js-module / clobbers属性。

From the root of your Cordova project, go into ./platforms/ios/ 从Cordova项目的根目录开始,进入./platforms/ios/

cd platforms/ios

In here there should be a ./build folder - this contains previously built packages which Cordova then uses for 'lazy loading' - ie so Cordova does not constantly have to fetch files from various locations, it stores them locally instead. 在这里应该有一个./build文件夹 - 这包含以前构建的软件包,然后Cordova用于“延迟加载” - 即Cordova不必经常从各个位置获取文件,而是将它们存储在本地。 However, I have found this to be problematic. 但是,我发现这是有问题的。

Delete this folder: 删除此文件夹:

sudo rm -r build

When gone, rebuild your Cordova project: 离开后,重建您的Cordova项目:

cordova build

It should then recreate the build folder but more importantly, your plugins will be listed in config.xml . 然后它应该重新创建构建文件夹,但更重要的是,您的插件将在config.xml列出。

Check you index.html file has the correct <script></script> lines. 检查index.html文件是否具有正确的<script></script>行。

I had an issue when creating a new project via CLI. 通过CLI创建新项目时遇到问题。 The index.html file referenced phonegap.js instead of cordova.js index.html文件引用了phonegap.js而不是cordova.js

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

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