Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Custom iOS Plugin For Phonegap

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 488
    Comment on it

    Hi All,

    A Plugin is a program, or a set of one or more functions, written in the objective-c/swift language, that adds a specific set of features or services to the Phonegap app, which can be seamlessly integrated with the Phonegap using access points and methods provided by the Phonegap plugin.

     

    Any iOS phonegap plugin directory structure look like :-

    iOS Phonegap plugin

    For Example if you making a plugin PluginCheck then this plugin structure should be:-

    In above directory structure color code used to define the following :- 

    Now, Our directory structure is setup completely. In coding part, first look into .xml file.

    <?xml version="1.0" encoding="utf-8"?>
    <plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
            id="com.example.plugincheck
            version="0.7.0">
    
      <name>Plugincheck</name>
    
      <engines>
        <engine name="cordova" version=">=3.4.0"/>
      </engines>
    
      <asset src="www/plugincheck.js" target="js/plugincheck.js"/>
    
      <js-module src="www/plugincheck.js" name="plugincheck">
        <clobbers target="plugincheck" />
      </js-module>
    
    
      <platform name="ios">
        
        <config-file target="config.xml" parent="/widget">
          <feature name="Plugincheck">
            <param name="ios-package" value=CDVPluginCheck />
          </feature>
        </config-file>
    
        <header-file src="src/ios/CDVPluginCheck.h" target-dir="Plugincheck"/>
        <source-file src="src/ios/CDVPluginCheck.m" target-dir="Plugincheck"/>
      </platform>
    
    </plugin>

    Above code is used for configuration of plugin and it is code of Plugin.xml file.

     

    Now look into javascript file . In www directory make a javascript file name plugincheck.js and add this code in  plugincheck.js file.

    cordova.define("com.example.plugincheck.plugincheck", function(require, exports, module) {
    
    /*global cordova, module*/
    
    module.exports = {
        check: function (name, successCallback, errorCallback) {
            cordova.exec(successCallback, errorCallback, "Plugincheck", "check", [name]);
        }
                   
    };

     

    Now look into Objective-C files . In src/ios directory make a objective-c file name CDVPluginCheck.h and add this code in  CDVPluginCheck.h file.

    #import <Cordova/CDV.h>
    
    @interface CDVPluginCheck : CDVPlugin
    
    - (void) check:(CDVInvokedUrlCommand*)command;
    
    @end

    Now look into Objective-C files . In src/ios directory make a objective-c file name CDVPluginCheck.m and add this code in  CDVPluginCheck.m file.

    #import "CDVPluginCheck.h"
    
    @implementation CDVPluginCheck
    
    - (void)check:(CDVInvokedUrlCommand*)command
    {
        NSString* callbackId = [command callbackId];
        NSString* name = [[command arguments] objectAtIndex:0];
         NSString* msg = [NSString stringWithFormat: @"Hello, %@", name];
    
        CDVPluginResult* result = [CDVPluginResult
                                   resultWithStatus:CDVCommandStatus_OK
                                   messageAsString:msg];
    
        [self.commandDelegate sendPluginResult:result callbackId:callbackId];
    }
    
    @end

    Now your plugin is ready to use. Just add your code to objective-c files and javscript files and add this plugin in your cordova project .

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: