Khadija Daruwala
Khadija Daruwala

Reputation: 1205

Creating and using Custom Cordova IOS plugin for Ionic App

I am very new to ionic development. I have an Objective C code that provides some custom functionality using the Camera and I want to use this native code in my ionic app using Cordova. After spending a few hours in the research I concluded that we need to create a Cordova plugin upload it on Github and then add it to our Ionic project. I tried I few demos and read some articles but all seem to be out of date and I am not able to proceed.

It would be really great if someone could guide me with

  1. Steps of creating a Cordova plugin with my Objective C code and
  2. Steps to use that plugin in my existing ionic app.

Any help would really be appreciated.

Upvotes: 1

Views: 1451

Answers (1)

Jonas Gann
Jonas Gann

Reputation: 61

Most information should be contained on the related Cordova website. Here are some boiled down leads ... Use cordova-plugman to create a cordova plugin like so:

  1. Install the package from github:
npm i -g git+https://[email protected]/apache/cordova-plugman.git
  1. Use the module to create a plugin like so:
plugman create --name "test" --plugin_id "test" --plugin_version "0.0.1"
  1. Change directory to folder "test" and run:
plugman createpackagejson .
  1. Add some platform the plugin should support:
plugman platform add --platform_name "android"

This should get you setup with a default plugin. To use the plugin from a Cordova app, change the directory to the app and run:

cordova plugin add /absolute/path/to/plugin

In the config.xml of the plugin, inside the following tag, you can specify how the plugin should be accessed during runtime.

<js-module name="test" src="www/test.js"><clobbers target="cordova.plugins.test" /></js-module>

In this case, the file www/test.js will be available under window.cordova.plugins.test. Reference is the Cordova documentation for the plugin.xml. Additional information on how to access native code on iOS can be found in the Cordova documentation for iOS plugin development.

Upvotes: 6

Related Questions