Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap plugin for ios returning FAILED pluginJSON

I'm working on a Phonegap/Cordova(version 2.9.0) custom plugin creation for iOS app. My steps are as follows :

  1. I created a HelloPlugin.js file and copy it under www/js/ folder, its having code :

    var HelloPlugin =
    {
      callNativeFunction: function (success, fail, resultType)
      {
        alert('a');
        return Cordova.exec( success, fail, "HelloPlugin", "nativeFunction", ['1']);
      }
    }; 
    
  2. I created HelloPlugin.h and HelloPlugin.m files under plugins folder, code :

    // .h
    #import <Cordova/CDVPlugin.h>
    
    @interface HelloPlugin : CDVPlugin
    
    - (void)nativeFunction:(CDVInvokedUrlCommand*)command;
    
    @end
    
    // .m
    
    #import "HelloPlugin.h"
    
    @implementation HelloPlugin
    
    - (void)nativeFunction:(CDVInvokedUrlCommand*)command
    {
      NSLog(@"Hello, this is a native function called from PhoneGap/Cordova!");
    }
    
    @end
    
  3. I added following code to config.xml file :

    <feature name="HelloPlugin">
      <param name="ios-package" value="CDVPlugin"/>
    </feature>
    
  4. At last I modified index.html in following way :

    1. Script reference added. ()
    2. JS code added :

        function callNativePlugin(returnSuccess) 
        {
           HelloPlugin.callNativeFunction( nativePluginResultHandler, nativePluginErrorHandler, returnSuccess );
        }
      
        function nativePluginResultHandler (result) {
              alert("SUCCESS: \r\n"+result );
        }
      
        function nativePluginErrorHandler (error) {
              alert("ERROR: \r\n"+error );
        } 
      
    3. Two buttons added and function called:

      "callNativePlugin('success');" "callNativePlugin('error');"

I hope this is the only required things I need to do for activating plugin.

Issue : While running the app, I am getting FAILED pluginJSON error on console.

Output :

-[CDVCommandQueue executePending] [Line 116] FAILED pluginJSON = [ "HelloPlugin2650437", "HelloPlugin", "nativeFunction", [ "1", "1", "1" ] ]

What mistake I have done, please let me know. I really Appreciate your efforts. Please help me here.

like image 440
Lalit Paliwal Avatar asked Aug 05 '13 13:08

Lalit Paliwal


2 Answers

First thing that jumped out is your plugin package name. It should be your iOS class name which is "HelloPlugin".

  <param name="ios-package" value="HelloPlugin"/>

One of the purpose of the new way of referencing plugin is to allow flexibility and backward compatibility of plugin names especially on Android. Example:

<feature name="HelloPlugin">
  <param name="ios-package" value="HelloCDVPlugin"/>
  <param name="android-package" value="com.phonegap.plugins.HelloCDVPlugin"/>
</feature>

Where "HelloCDVPlugin" is your iOS class name and "com.phonegap.plugins.HelloCDVPlugin" is your Android class name.

like image 123
Noogen Avatar answered Nov 01 '22 11:11

Noogen


Use below code for js

cordova.define("cordova/plugin/hello",
function (require, exports, module) {

var exec = require('cordova/exec');

function greet(name, win, fail) {
  exec(win, fail, "Hello",
      "greet", [name]);
}

module.exports = {
  greet: greet
}
}
);

and change cordova.exec to

exec(this.callbacks.onSuccess, this.callbacks.onError, "Hello", "greet", [defaults]);

You can find example from below link

https://github.com/cristobal/phonegap-ios-datepicker-plugin

You have to modify the code as per your requirement.

like image 44
Bhavesh Parekh Avatar answered Nov 01 '22 09:11

Bhavesh Parekh