Hey there! As a Capacitor supplier, I often get asked about how to check the network status in a Capacitor app. It’s a crucial feature, especially when you’re building an app that relies on internet connectivity for things like data syncing, real – time updates, or accessing cloud – based services. In this blog, I’ll walk you through the steps and share some tips on making it happen smoothly. Capacitor

Why Checking Network Status Matters
Before we dive into the how – to, let’s quickly talk about why it’s so important. When your app tries to perform an action that requires an internet connection, like sending a user’s data to a server, and there’s no network available, it can lead to a really bad user experience. The app might freeze, crash, or just give an unhelpful error message. By checking the network status beforehand, you can give users a heads – up, offer alternative actions, or gracefully handle the lack of connectivity.
Prerequisites
First off, you need to have a basic Capacitor project up and running. If you’re new to Capacitor, it’s a cross – platform native runtime that makes it easy to build web apps and turn them into native iOS and Android apps. You’ll also need Node.js and npm installed on your machine. If not, head over to the official Node.js website and get them set up.
Installing the Network Plugin
Capacitor has a great network plugin that makes it super easy to check the network status. To install it, open up your terminal and navigate to your project directory. Then run the following command:
npm install @capacitor/network
npx cap sync
The npm install command adds the network plugin to your project’s dependencies, and npx cap sync updates your native projects (iOS and Android) to include the newly installed plugin.
Checking the Network Status in Code
JavaScript/TypeScript
Once the plugin is installed, you can start using it in your code. Here’s a simple example of how to check the network status in JavaScript or TypeScript:
import { Plugins } from '@capacitor/core';
const { Network } = Plugins;
async function checkNetworkStatus() {
try {
const status = await Network.getStatus();
console.log('Network status:', status);
if (status.connected) {
console.log('You are connected to the network!');
// Here you can perform actions that require internet
} else {
console.log('You are not connected to the network.');
// You might want to show a message to the user or disable certain features
}
} catch (error) {
console.error('Error getting network status:', error);
}
}
checkNetworkStatus();
In this code, we first import the Network plugin from @capacitor/core. Then we define an asynchronous function checkNetworkStatus that calls Network.getStatus(). This function returns a promise that resolves with an object containing information about the network status, like whether the device is connected and the type of connection (WiFi, cellular, etc.).
React Example
If you’re using React in your Capacitor app, here’s how you can integrate the network check into a functional component:
import React, { useEffect } from 'react';
import { Plugins } from '@capacitor/core';
const { Network } = Plugins;
const NetworkStatusComponent = () => {
useEffect(() => {
const checkStatus = async () => {
try {
const status = await Network.getStatus();
if (status.connected) {
console.log('Connected to the network!');
} else {
console.log('Not connected to the network.');
}
} catch (error) {
console.error('Error getting network status:', error);
}
};
checkStatus();
}, []);
return (
<div>
{/* You can add UI elements here to show the network status */}
</div>
);
};
export default NetworkStatusComponent;
In this React example, we use the useEffect hook to call the network status check when the component mounts.
Listening for Network Changes
It’s not just about checking the network status once. You might also want to listen for changes in the network status, like when the user switches from WiFi to cellular data or loses connection altogether. The Network plugin makes this easy too.
import { Plugins } from '@capacitor/core';
const { Network } = Plugins;
const networkStatusListener = Network.addListener('networkStatusChange', (status) => {
console.log('Network status changed:', status);
if (status.connected) {
console.log('Reconnected to the network!');
// You can do things like resume data syncing
} else {
console.log('Disconnected from the network.');
// Pause actions that require internet
}
});
// Don't forget to remove the listener when you're done
// For example, in a React component's cleanup
const removeListener = () => {
networkStatusListener.remove();
};
In this code, we use Network.addListener to listen for the networkStatusChange event. Whenever the network status changes, the callback function is called with the new status object.
Handling Different Platforms
One of the great things about Capacitor is that it abstracts away a lot of the platform – specific differences. However, there are still some things to keep in mind.
iOS
On iOS, the network status check works out of the box once you’ve installed and synced the plugin. But you need to make sure your app has the necessary privacy permissions if you’re using the network for things like location – based services.
Android
On Android, the plugin also works well, but you need to add the appropriate permissions to your AndroidManifest.xml file. You’ll need the ACCESS_NETWORK_STATE permission to check the network status. You can add it like this:
<uses - permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Tips and Best Practices
- Graceful Degradation: When the network is unavailable, design your app to still be usable. For example, you can show cached data or allow users to perform offline actions.
- Error Handling: Always handle errors when getting the network status. Network calls can fail for various reasons, and you don’t want your app to crash because of it.
- Testing: Test your app on different network conditions, like WiFi, cellular, and no network at all. This will help you catch any issues early on.
Conclusion

Checking the network status in a Capacitor app is a vital feature that can greatly enhance the user experience. By using the @capacitor/network plugin and following the steps and best practices I’ve outlined, you can easily implement this functionality in your app.
Vacuum Relay As a Capacitor supplier, we’re here to help you build amazing apps. Whether you’re new to Capacitor or looking to add more advanced features to your existing app, we’ve got the expertise to support you. If you’re interested in learning more about our services or have any questions about integrating the network status check or other Capacitor features into your app, don’t hesitate to reach out. We’re ready to have a chat and see how we can work together to take your app to the next level.
References
- Capacitor official documentation
- Node.js official documentation
- React official documentation
Jingdezhen Wanping Electric Co., Ltd.
As one of the most professional capacitor manufacturers and suppliers in China, we also support customized service. We warmly welcome you to buy high quality capacitor made in China here and get pricelist from our factory. For price consultation, contact us.
Address: Zhangshukeng, Jingdezhen City, Jiangxi Province.
E-mail: jdzwpdq0815@163.com
WebSite: https://www.cewpdq.com/