Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Cordova plugin exists

Is there a way to tell if a specific Cordova plugin is available to be called in Cordova 2.5.0? Cordova.exec takes in a successFunction and a failFunction but the failFunction is only called if the native code executes. Is there a way to receive an error or callback if the receiving function doesn't exist? I'm looking for a solution that works with a remove URL loaded into WebView

http://docs.phonegap.com/en/2.5.0/guide_plugin-development_ios_index.md.html

like image 332
sguha Avatar asked Jul 26 '13 00:07

sguha


People also ask

How do I know if cordova is available?

Add an event listener (load) for the cordova script that creates a boolean variable in the window ( usingCordova ) initially set to false and then once the cordova script loads, it will be set to true. Then you can use a simple if statement verifying wheter cordova was loaded or not.

How do I check my ionic plugins version?

To find the locally installed version of Cordova CLI run cordova -v (If you are using Ionic, this version is also included in the ionic info output). Then compare the returned version number to the output of npm info cordova version (You can also check manually for the newest available version on npm or GitHub).


2 Answers

For a quite simpler (and cleaner) solution, you can also use this method :

var plugins = cordova.require("cordova/plugin_list").metadata;

You'll get an object looking like that :

{
    'cordova-plugin-network-information': "1.0.0",
    'cordova-plugin-whitelist': "1.0.0"
}

As any normal variable, you can then check if it's defined :

if (typeof plugins['cordova-plugin-whitelist'] === "undefined")
{
    // This plugin is missing
}
like image 131
Ivan Gabriele Avatar answered Sep 29 '22 21:09

Ivan Gabriele


you can do a setTimeout for 100 ms before trying to access the plugin, and on success/failure of the plugin cancel the timeout. if the timeout triggers - the plugin is not installed.

like image 32
shaish Avatar answered Sep 29 '22 21:09

shaish