Installing Plugman and creating Cordova plugin (iOS) using Swift — Part Two
In the Part One we completed till adding the iOS platform in our plugin temple created by Plugman Tool. As we saw by default the DemoPlugin.m file was created and since we need the Swift file we will be deleting the .m file and creating a DemoPlugin.swift file. Inside the DemoPlugin.swift file let as add the sample program of adding two numbers. The code will be something like:
The file can be seen at GitHub link
STEP 4 : DemoPlugin.js
Open the “www” folder and there we can see DemoPlugin.js file. Register the methods that can be used from the plugin in the DemoPlugin.js file of the plugin.
module.exports.add = function (arg0, success, error) {exec(success, error, 'DemoPlugin', 'add', [arg0]);};
STEP 6: Create package.json
Now create a package.json file for the plugin using the following command and answer all the questions asked. The command for this will be:
npm init
STEP 7: Changing the clobbers value
Changes needed to be done in plugin.xml
1.Open the plugin.xml and change the clobbers target value in the plugin.xml file as this value can be used to refer to the plugin in your project.
2. Since we had modified the .m file to .swift we need to change that too in the plugin .xml
<clobbers target="DemoPlugin"/>
</config-file><source-file src="src/ios/DemoPlugin.swift" />
Thats all… Now we will add the plugin to our Cordova project.
CREATING THE CORDOVA PROJECT
STEP 1: Installing the Cordova CLI
The Cordova command-line tool is distributed as an npm package.
To install the cordova
command-line tool, follow these steps:
- Download and install Node.js. On installation you should be able to invoke
node
andnpm
on your command line.
2. (Optional) Download and install a git client, if you don’t already have one. Following installation, you should be able to invoke git
on your command line. The CLI uses it to download assets when they are referenced using a url to a git repo.
3. Install the cordova
module using npm
utility of Node.js. The cordova
module will automatically be downloaded by the npm
utility.
on OS X and Linux:
sudo npm install -g cordova
or
npm install -g cordova
STEP 2: Create the App
Go to the directory where you maintain your source code, and create a cordova project:
$ cordova create hello com.example.hello HelloWorld
This creates the required directory structure for your cordova app. By default, the cordova create
script generates a skeletal web-based application whose home page is the project's www/index.html
file.
STEP 3: Add Platforms
All subsequent commands need to be run within the project’s directory, or any subdirectories:
$ cd hello
Add the platforms that you want to target your app. Cordova will add the ‘ios’ and ‘android’ platform and ensure they get saved to config.xml
and package.json
:
$ cordova platform add ios
$ cordova platform add android
To check your current set of platforms:
$ cordova platform ls
STEP 4: ADDING CUSTOM PLUGIN
For adding the custom plugin we need to execute the command :
cordova plugin add *path_of_plugin*
Here path can be either our github link were the plugin is hoisted or our local path. Since our DemoPlugin is in our local machine we will give the local path by just dragging the folder to the Terminal so our final command will be :
cordova plugin add /Users/Mymac/Downloads/Plugman\ Plugins/DemoPlugin
Press enter and see the magic our custom plugin will be installed in our cordova ios project.
STEP 5: Listing our plugins installed
We can verify whether our plugin is installed or not by running the command:
cordova plugin list
Thats all for today i hope this two part tutorial atleast gave you an idea of how to create the cordova project with our custom built plugin using swift.
Keep enjoying coding :)