What is @capacitor/core?
@capacitor/core is a cross-platform native runtime for building web applications that run natively on iOS, Android, and the web. It provides a consistent API for accessing native device features and allows developers to write their application code once and deploy it across multiple platforms.
What are @capacitor/core's main functionalities?
Accessing Device Information
This feature allows you to access detailed information about the device, such as the operating system, model, and manufacturer.
const { Device } = require('@capacitor/device');
async function getDeviceInfo() {
const info = await Device.getInfo();
console.log(info);
}
getDeviceInfo();
Geolocation
This feature allows you to access the device's current location using GPS and other location services.
const { Geolocation } = require('@capacitor/geolocation');
async function getCurrentPosition() {
const coordinates = await Geolocation.getCurrentPosition();
console.log('Current position:', coordinates);
}
getCurrentPosition();
Camera
This feature allows you to access the device's camera to take photos or capture video.
const { Camera, CameraResultType } = require('@capacitor/camera');
async function takePicture() {
const image = await Camera.getPhoto({
quality: 90,
allowEditing: false,
resultType: CameraResultType.Uri
});
console.log('Image URI:', image.webPath);
}
takePicture();
Storage
This feature allows you to store and retrieve data locally on the device.
const { Storage } = require('@capacitor/storage');
async function storeData() {
await Storage.set({ key: 'name', value: 'Capacitor' });
const { value } = await Storage.get({ key: 'name' });
console.log('Stored value:', value);
}
storeData();
Other packages similar to @capacitor/core
cordova
Apache Cordova is a mobile application development framework that allows you to use standard web technologies such as HTML5, CSS3, and JavaScript for cross-platform development. It provides a set of plugins to access native device features, similar to @capacitor/core, but Capacitor offers a more modern API and better integration with modern web development practices.
react-native
React Native is a framework for building native apps using React. It allows you to write your application in JavaScript and render it using native components. While React Native provides a more native-like experience and performance, @capacitor/core allows you to use web technologies and provides a consistent API across platforms.
flutter
Flutter is a UI toolkit from Google for building natively compiled applications for mobile, web, and desktop from a single codebase. It uses the Dart programming language. While Flutter provides a rich set of pre-designed widgets and high performance, @capacitor/core allows you to leverage existing web development skills and libraries.