简体   繁体   English

将自定义cordova插件添加到IBM Worklight 6.1

[英]Add custom cordova plugin to IBM Worklight 6.1

I am trying to add a custom Cordova plugin for the iOS platform, and I am having some issues when I compare that with the process to add a plugin on cordova. 我正在尝试为iOS平台添加一个自定义Cordova插件,当我将其与在cordova上添加插件的过程进行比较时,我遇到了一些问题。

The plugin I am trying to use here is https://github.com/phonegap-build/StatusBarPlugin 我试图在这里使用的插件是https://github.com/phonegap-build/StatusBarPlugin

With cordova I used to simply use the command line c ordova plugin add com.phonegap.plugin.statusbar 使用cordova我以前只使用命令行c ordova plugin add com.phonegap.plugin.statusbar

First, I tried to modify in native folder, but I noticed that If I do so, It works but It will be erased the next time I deploy again for iOS platform. 首先,我尝试在原生文件夹中进行修改,但是我注意到如果我这样做,它可以工作但下次我再次部署iOS平台时会被删除。 Second, I tried to add files (plugin js file and cordova_plugins.js file.) under apps/myapp/iphone, or apps/myapp/common, but this causes an issue : The cordova_plugins.js file format seems to become not ok. 其次,我尝试在apps / myapp / iphone或apps / myapp / common下添加文件(插件js文件和cordova_plugins.js文件。),但这会导致一个问题: cordova_plugins.js文件格式似乎cordova_plugins.js

Instead of having this working format: 而不是具有这种工作格式:

cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.battery-status/www/battery.js",
        "id": "org.apache.cordova.battery-status.battery",
        "clobbers": [
            "navigator.battery"
        ]
    },
,
    {
        "file": "plugins/com.phonegap.plugin.statusbar/www/statusbar.js",
        "id": "com.phonegap.plugin.statusbar.statusbar",
        "clobbers": [
            "window.StatusBar"
        ]
    }
]
});

It have this format that does not work properly : 它具有无法正常工作的格式:

/* JavaScript content from worklight/cordova_plugins.js in JS Resources */
/*
* Licensed Materials - Property of IBM
* 5725-I43 (C) Copyright IBM Corp. 2006, 2013. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.battery-status/www/battery.js",
        "id": "org.apache.cordova.battery-status.battery",
        "clobbers": [
            "navigator.battery"
        ]
    }
]
});
/* JavaScript content from worklight/cordova_plugins.js in folder common */

/* JavaScript content from worklight/cordova_plugins.js in JS Resources */
/*
* Licensed Materials - Property of IBM
* 5725-I43 (C) Copyright IBM Corp. 2006, 2013. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.battery-status/www/battery.js",
        "id": "org.apache.cordova.battery-status.battery",
        "clobbers": [
            "navigator.battery"
        ]
    },
    {
        "file": "plugins/com.phonegap.plugin.statusbar/www/statusbar.js",
        "id": "com.phonegap.plugin.statusbar.statusbar",
        "clobbers": [
            "window.StatusBar"
        ]
    }
]
});

How should I do? 我应该怎么做? Where should I put these file? 我应该把这些文件放在哪里? What is the proper way to add this custom plugin, especially if I want to add it only for iOS and not for Android? 添加此自定义插件的正确方法是什么,特别是如果我只想为iOS而不是Android添加它?

UPDATE: As of MobileFirst 7.1, the SDK is now available as a Cordova plugin. 更新:从MobileFirst 7.1开始,SDK 现在可作为Cordova插件使用。

For those interested in adding 3rd party plugins to their MobileFirst (Worklight) projects I have described my own approach to installing them below, pending a feature release from IBM. 对于那些有兴趣在他们的MobileFirst(Worklight)项目中添加第三方插件的人,我已经描述了我自己在下面安装它们的方法,等待IBM的功能发布。

The concept is essentially: 这个概念基本上是:

  1. create a Cordova project, 创建一个Cordova项目,
  2. add the desired plugin, 添加所需的插件,
  3. create a MobileFirst project, 创建一个MobileFirst项目,
  4. copy the plugin files from both projects to a staging area (so we can easily identify them), 将插件文件从两个项目复制到临时区域(这样我们就可以轻松识别它们),
  5. merge the config.xml and cordova_plugin.js files (ie supplement the MobileFirst files with the plugin information from the Cordova project) and 合并config.xml和cordova_plugin.js文件(即使用Cordova项目中的插件信息补充MobileFirst文件)和
  6. copy the staged and modified files to MobileFirst. 将暂存和修改的文件复制到MobileFirst。

Disclaimer: as per the accepted answer, IBM do not support/advise modifying the cordova_plugin.js file. 免责声明:根据公认的答案,IBM不支持/建议修改cordova_plugin.js文件。

Firstly we need to create the Cordova project (plus the plugin) and MobileFirst projects (steps 1-4). 首先,我们需要创建Cordova项目(加上插件)和MobileFirst项目(步骤1-4)。 I've used the Ionic Keyboard plugin as an example, needless to say this approach (creating a Cordova project and merging files) works for any supported plugin and target. 我使用了Ionic Keyboard插件作为示例,不用说这种方法(创建Cordova项目和合并文件)适用于任何受支持的插件和目标。

## Create a directory to contain your MobileFirst project e.g. mkdir example; cd example; ##
## Create Cordova project ##
mkdir .tmp
cd .tmp/
cordova create plugins com.plugins plugins;
cd plugins/
cordova platform add ios;
cordova plugin add com.ionic.keyboard;
cd ../..
## Create mobile first project ##
mfp create hybrid
cd hybrid/
mfp add hybrid hybrid
mfp add environment iphone
## Generate native files ##
mfp build
cd ..
## Create staging ##
mkdir -p plugins/native/www/default/worklight
mkdir -p plugins/resources/mobilefirst/
mkdir -p plugins/resources/cordova/
mkdir -p plugins/hm/
## Copy config.xml ##
cp hybrid/apps/hybrid/iphone/native/config.xml plugins/resources/mobilefirst/
cp .tmp/plugins/platforms/ios/plugins/config.xml plugins/resources/cordova/
## Copy Cordova files ##
cp -R hybrid/apps/hybrid/iphone/native/www/default/worklight/ plugins/resources/mobilefirst/
## Copy plugins JS ##
cp -R .tmp/plugins/platforms/ios/www/plugins plugins/native/www/default/worklight/
cp -R .tmp/plugins/platforms/ios/www/ plugins/resources/cordova/
## Copy classes ##
cp -R .tmp/plugins/platforms/ios/Plugins/Plugins/com.ionic.keyboard/ plugins/hm/
## Delete the Cordova project as we have copied all of the artefacts we need ##
rm -R .tmp
## Create the config and cordova_plugin.js which is going to override the mfp build version ##
cp plugins/resources/mobilefirst/config.xml plugins/native/
cp plugins/resources/mobilefirst/cordova_plugins.js plugins/native/www/default/worklight/

The staged config.xml and cordova_plugins.js files are now ready to merge (step 5). 现在已准备好分阶段的config.xml和cordova_plugins.js文件(步骤5)。

Open the plugins/resources/cordova/config.xml file and copy the feature into the plugins/native/config.xml file. 打开plugins / resources / cordova / config.xml文件,将该功能复制到plugins / native / config.xml文件中。

<feature name="Keyboard">
  <param name="ios-package" onload="true" value="IonicKeyboard" />
</feature>

Open the plugins/resources/cordova/cordova_plugins.js file and copy the plugin object into the plugins/native/www/default/worklight/cordova_plugins.js file. 打开plugins / resources / cordova / cordova_plugins.js文件,将插件对象复制到plugins / native / www / default / worklight / cordova_plugins.js文件中。

{
  "file": "plugins/com.ionic.keyboard/www/keyboard.js",
  "id": "com.ionic.keyboard.keyboard",
  "clobbers": [
    "cordova.plugins.Keyboard"
  ]
}

Now we are ready to copy the merged files into the MobileFirst project (step 6a). 现在我们准备将合并的文件复制到MobileFirst项目中(步骤6a)。

## Copy from staging to Worklight ##
cp -R plugins/hm/ hybrid/apps/hybrid/iphone/native/Classes/

The first time you copy the files Xcode won't pickup the new classes automatically, so open up the project in Xcode and right click on Classes and 'Add files to ...'. 第一次复制文件Xcode不会自动拾取新类,因此在Xcode中打开项目并右键单击Classes和'Add files to ...'。 Add the files displayed in the dialog. 添加对话框中显示的文件。

Finally, we can copy the files from the plugins/native directory into the MobileFirst project (step 6b). 最后,我们可以将插件/本机目录中的文件复制到MobileFirst项目中(步骤6b)。 Unfortunately we need to copy this directory after every mfp build , as mfp resets the cordova_plugins.js file each time. 不幸的是,我们需要在每次mfp构建之后复制此目录,因为mfp每次都会重置cordova_plugins.js文件。

## Do this after every mfp build ##
rm -f hybrid/apps/hybrid/iphone/native/www/default/worklight/cordova_plugins.js
cp -R plugins/native/ hybrid/apps/hybrid/iphone/native/

Once complete, add the client code to your hybrid application and test (don't forget to run step 6 again after the mfp build) eg 完成后,将客户端代码添加到混合应用程序并进行测试(不要忘记在mfp构建后再次运行步骤6),例如

<input type="text">

window.addEventListener('native.keyboardshow', keyboardShowHandler);

function keyboardShowHandler(e){
    alert('Keyboard height is: ' + e.keyboardHeight);
}

I hope this guide proves useful. 我希望这本指南证明是有用的。 I use this process on a daily basis (albeit as part of Grunt) and look forward to a feature release from IBM. 我每天都使用这个过程(尽管是Grunt的一部分),并期待IBM发布一个功能。

Worklight 6.1.0.x does not yet support adding pre-made Cordova 3.x plug-ins using plugman or any other procedure you would do in a pure Cordova application, including trying to edit the file you're trying to edit. Worklight 6.1.0.x还不支持使用plugman或在纯Cordova应用程序中执行的任何其他过程添加预制的Cordova 3.x插件,包括尝试编辑您尝试编辑的文件。 This is a known limitation in current versions of Worklight. 这是当前版本的Worklight中的已知限制。

What I would do is read the training material of creating Cordova plug-ins in Worklight, and then take the source of the plug-in you want to add and copy it over. 我要做的是阅读在Worklight 中创建Cordova插件的培训材料 ,然后获取要添加的插件的来源并将其复制。

There are probably other ways to do it, but none is convenient at this time. 可能有其他方法可以做到,但目前没有方便。

There is a issue with the above mentioned solution by Chris. 克里斯提出上述解决方案存在问题。 The plugin would surely work if you follow the process correctly but there will be severe consequences later. 如果你正确地遵循这个过程,插件肯定会起作用,但之后会有严重的后果。 Eg Since the plugins folder and the cordova_plugins.js file will be overwritten everytime there is a mfp build, the wlapp files generated will never have the plugin code added. 例如,因为每次有mfp构建时都会覆盖plugins文件夹和cordova_plugins.js文件,所以生成的wlapp文件永远不会添加插件代码。 Hence if you are using direct update your code will be overwritten once you upload this wlapp file and the plugin would stop working. 因此,如果您使用直接更新,一旦您上传此wlapp文件,您的代码将被覆盖,并且该插件将停止工作。

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

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