4

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.

The plugin I am trying to use here is https://github.com/phonegap-build/StatusBarPlugin

With cordova I used to simply use the command line cordova 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. 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.

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?

WiPhone
  • 683
  • 6
  • 24
  • I will look into this more but based on my test now, the rebuild only removes the plugin.js files but will keep the native Objective-C code and the change to the config.xml. So you could still use the plugin. I would suggest copy the plugin js file content to your main js file so that you could keep them between builds. – jiachen May 16 '14 at 21:47
  • As for using plugin based on device type, I think the easiest way is to use the Device plugin (http://docs.phonegap.com/en/3.0.0/cordova_device_device.md.html) to choose which plugin to use based on the device type. – jiachen May 16 '14 at 21:51
  • @jiachen thanks for your suggestion, I tried adding cordova_plugins.js content to main.js, but it seems that it does not work. I am now trying to erase this file with buildtime.sh script, but still having issues. For using Device Plugin, yes I will definitely do that – WiPhone May 17 '14 at 08:50
  • Finally, I used cordova.exec instruction, because, even if I 've found a workaround, it does not work after direct update. – WiPhone May 19 '14 at 08:07

3 Answers3

2

UPDATE: As of MobileFirst 7.1, the SDK is now available as a Cordova plugin.

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.

The concept is essentially:

  1. create a Cordova project,
  2. add the desired plugin,
  3. create a MobileFirst project,
  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 (i.e. supplement the MobileFirst files with the plugin information from the Cordova project) and
  6. copy the staged and modified files to MobileFirst.

Disclaimer: as per the accepted answer, IBM do not support/advise modifying the cordova_plugin.js file.

Firstly we need to create the Cordova project (plus the plugin) and MobileFirst projects (steps 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.

## 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).

Open the plugins/resources/cordova/config.xml file and copy the feature into the plugins/native/config.xml file.

<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.

{
  "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).

## 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 ...'. Add the files displayed in the dialog.

Finally, we can copy the files from the plugins/native directory into the MobileFirst project (step 6b). Unfortunately we need to copy this directory after every mfp build, as mfp resets the cordova_plugins.js file each time.

## 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) e.g.

<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.

Chris
  • 81
  • 4
  • Hi @Chris, thanks for your answer! I think it shoukd work if you automate this (maybe creating ant tasks or so) but I've a doubt about the process of creating the checksum for passing the App Authenticity Check later. Have you tested this? – Floydian Jul 13 '15 at 13:01
  • @Floydian thanks. I haven't tested this approach with the authenticity check. As the signing takes place after the modifications that shouldn't be an issue; however, encrypted web resources may be a problem as you suggest. As per my post, this is an unsupported hack, as such I wouldn't encourage doing this, unless you really have to. Never fear though, MobileFirst 7.1 is due soon, with 3rd party plugin support! – Chris Aug 12 '15 at 04:36
1

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. This is a known limitation in current versions of 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.

There are probably other ways to do it, but none is convenient at this time.

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • The sample provided uses directly cordova.exec() functions. I want to add a plugin module, with its function, like in my Case StatusBar module, with functions like StatusBar.show() and StatusBar.hide() I am trying to erase cordova_plugins.js file on iOS Xcode build time, but I am having this error now : cp: /Volumes/.../iphone/native/www/default/worklight/cordova_plugins.js: Permission denied | and I am adding the following line to the script : cp "${SRCROOT}/cordova_plugins.js" "${SRCROOT}/www/default/worklight/cordova_plugins.js" – WiPhone May 17 '14 at 08:56
  • You CANNOT edit this file. Like I said, if you write your own plug-in you do not need to edit cordova_plugins.js; read the training material. You need to create a new plug-in and declare it in config.xml. You may need to do heavy editing to this status bar plug-in you want. It will not work as-is out of the box. – Idan Adar May 17 '14 at 08:59
  • I see, that can be a big limitation, In this case I can may be change the usage of this plugin and rewrite functions that communicate with it, but what about other complicated plugins. I believe it really important to find at least a workaround – WiPhone May 17 '14 at 09:02
  • I agree. This is known limitation that will be addressed in a future release of Worklight. – Idan Adar May 17 '14 at 09:04
  • 1
    Hi Adar, is this limitation fixed in Worklight 6.2.0.x ? I am currently facing the issue to set the status bar styling to light content. Can you provide me exact link to creating cordova plug-ins? The link you mentioned above is pointing to getting started page. – Javal Nanda Sep 04 '14 at 04:09
  • The limitation is still there. Please, next time... just read the page. You'll see text saying "creating cordova plugins"........... http://www.ibm.com/developerworks/mobile/worklight/getting-started.html#GS_native_to_hybrid – Idan Adar Sep 04 '14 at 04:11
  • @WiPhone can you share me details regarding how you manage to set status bar style to light content using this cordova plugin for status bar – Javal Nanda Sep 05 '14 at 09:05
  • @IdanAdar which version of phonegap is used by worklight v6.3? – Daniel Yang Dec 12 '14 at 04:13
  • In MobileFirst Platform Foundation 6.3 (formerly "IBM Worklight Foundation), Cordova 3.6 is used. – Idan Adar Dec 12 '14 at 04:15
0

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. E.g 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. Hence if you are using direct update your code will be overwritten once you upload this wlapp file and the plugin would stop working.

  • Whilst the scenario you describe can be resolved by a simple extension of this method (the wlapp files are just zips after all), this entire approach is an **unsupported workaround**, and does have limitations; for example see [IBM's post](https://developer.ibm.com/mobilefirstplatform/2015/08/03/integrating-3rd-party-cordova-plug-ins/). As such, your point is well made. That being said, MobileFirst 7.1 is due any day now and is expected to ship with support for 3rd party plugins, rendering this hack moot. – Chris Aug 12 '15 at 04:29