DEV Community

Alex Spinov
Alex Spinov

Posted on

Capacitor Has a Free API That Most Developers Dont Know About

Capacitor by Ionic lets you build cross-platform mobile apps using web technologies. Unlike Cordova, it gives you full native API access with a modern plugin system.

Native APIs from JavaScript

import { Camera, CameraResultType } from "@capacitor/camera";
import { Geolocation } from "@capacitor/geolocation";
import { LocalNotifications } from "@capacitor/local-notifications";
import { Share } from "@capacitor/share";

// Take a photo
const photo = await Camera.getPhoto({
  quality: 90,
  resultType: CameraResultType.Uri,
  allowEditing: true
});

// Get location
const position = await Geolocation.getCurrentPosition();
console.log(position.coords.latitude, position.coords.longitude);

// Local notification
await LocalNotifications.schedule({
  notifications: [{
    title: "Reminder",
    body: "Check your tasks",
    id: 1,
    schedule: { at: new Date(Date.now() + 60000) }
  }]
});

// Share
await Share.share({
  title: "Check this out",
  text: "Amazing app!",
  url: "https://example.com"
});
Enter fullscreen mode Exit fullscreen mode

Custom Native Plugins

import { registerPlugin } from "@capacitor/core";

interface MyPluginInterface {
  echo(options: { value: string }): Promise<{ value: string }>;
}

const MyPlugin = registerPlugin<MyPluginInterface>("MyPlugin");
const result = await MyPlugin.echo({ value: "Hello" });
Enter fullscreen mode Exit fullscreen mode

Setup

npm install @capacitor/core @capacitor/cli
npx cap init
npx cap add ios
npx cap add android
npx cap sync
npx cap open ios  # Opens Xcode
Enter fullscreen mode Exit fullscreen mode

Key Features

  • Full native API access from JS/TS
  • Works with any web framework (React, Vue, Angular, Svelte)
  • Native plugin system with Swift/Kotlin
  • Live reload during development
  • PWA support built-in

Need to scrape or monitor web data at scale? Check out my web scraping actors on Apify or email spinov001@gmail.com for custom solutions.

Top comments (0)