cordova custom plug-in (iOS)

1.plugman installation
npm install -g plugman
If permission denied is prompted, sudo should be added
sudo npm install -g plugman
2. Create plug-ins
Sudo plugin create -- name plug-in name -- plugin UU ID plug-in ID -- plugin UU version 0.0.1

3 create package.json file
Under the plug-in folder of the bedstead on cd, plugman createpackagejson [plug-in path]

4. Open TestPlugin folder, add ios folder under src, and add plug-in class in ios file

The class of plug-in should inherit to CDVPlugin

#import <Cordova/CDVPlugin.h>

@interface PluginDemo : CDVPlugin

- (void)doTest:(CDVInvokedUrlCommand *)command;


Output only one sentence to check whether our plug-in can be activated

#import "PluginDemo.h"

@implementation PluginDemo

- (void)doTest:(CDVInvokedUrlCommand *)command {
    NSLog(@"The plug-in is activated");

5. Configure plugin.xml in the generated folder above

6. Configure TestPlugin.js under www file

Custom plug-in complete

We have a new cordova project called iosApp
Import our plug-in into the cordova project to test
sudo cordova plugin add [plugin path]

Start project add button in index.html to check whether plug-in can be started

Code in index.html

<!DOCTYPE html>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *;script-src * 'unsafe-inline'">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
        <div class="app">
            <h1>Apache Cordova</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            <Button onclick="TestPlugin.doTest()">test</Button>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>

Tags: sudo npm iOS Javascript

Posted on Sat, 30 Nov 2019 14:36:59 -0800 by dnoonan