npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

demo-capacitor-plugin

v0.1.0

Published

An example of a custom Capacitor plugin

Readme

Capacitor Plugin Demo

An example of a Capacitor plugin which make native Swift and Java code available for use in Typescript for an Ionic project

TypeScript Interface

Open src/definitions.ts

declare module "@capacitor/core" {
  interface PluginRegistry {
    DemoPlugin: DemoPluginPlugin;
  }
}

export interface DemoPluginPlugin {
  sum(options: { first: number, second: number}): Promise<{value: number}>;
}

Example of exposing native code

iOS

Open ios/Plugin/Plugin.swift

A Capacitor plugin for iOS is a simple Swift class that extends CAPPlugin and has some exported methods that will be callable from Typescript.

@objc(DemoPlugin)
public class DemoPlugin: CAPPlugin {
    
    @objc func sum(_ call: CAPPluginCall) {
        guard let first = call.getInt("first"),
            let second = call.getInt("second") else {
            call.reject("Received invalid input")
            return
        }
        let sum = first + second
        call.success(["sum": sum])
    }
}

Each plugin method receives an instance of CAPPluginCall containing all the information of the plugin method invocation from the client.

Export to Capacitor

Open ios/Plugin/Plugin.m

To make sure Capacitor can see your plugin, you must do two things: export your Swift class to Objective-C, and register it using the provided Capacitor Objective-C Macros.

CAP_PLUGIN(DemoPlugin, "DemoPlugin",
           CAP_PLUGIN_METHOD(sum, CAPPluginReturnPromise);

Android

Open android/src/main/java/com/louisdebaere/demo/plugin/DemoPlugin.java

A Capacitor plugin for Android is a simple Java class that extends com.getcapacitor.Plugin and have a @NativePlugin annotation. It has some methods with @PluginMethod() annotation that will be callable from Typescript.

@NativePlugin()
public class DemoPlugin extends Plugin {

    @PluginMethod()
    public void sum(PluginCall call) {
        Integer first = call.getInt("first");
        Integer second = call.getInt("second");
        Integer sum = first + second;
        JSObject ret = new JSObject();
        ret.put("sum", sum);
        call.success(ret);
    }
}

Each plugin method receives an instance of com.getcapacitor.PluginCall containing all the information of the plugin method invocation from the client.

Export to Capacitor

By using the @NativePlugin and @PluginMethod() annotations in your plugins, you make them available to Capacitor, but you still need an extra step in your application to make Capacitor aware of the plugins.

This is done in your apps MainActivity, where you add it in e.g. src/main/java/com/example/myapp/MainActivity.java like so:

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(DemoPlugin.class);
    }});
  }
}