Mobile App Development with Ionic React and Capacitor

Ionic React App Development: Building and Executing an Ionic React App with Capacitor Integration

Pre-requirements

Before we dive into developing an Ionic React app with Capacitor integration, make sure you have the following tools installed:

  • Node.js and npm (Node Package Manager): You can download and install them from nodejs.org.
  • Ionic CLI: The Ionic Command Line Interface is used for creating and managing Ionic projects. Install it globally using npm:
npm install -g @ionic/cli

Native-run and Cordova-res: These are essential for running your app on native devices. Install them globally as well:

npm install -g native-run cordova-res

Creating the Ionic React App

Let’s create our Ionic React app using the “tabs” template and integrate Capacitor for native functionality:

ionic start photo-gallery tabs --type=react --capacitor

This command generates an Ionic React project named “photo-gallery” based on the “tabs” template, with Capacitor integration.

Installing Required Capacitor Plugins

Next, we need to install the necessary Capacitor plugins for native functionality. We’ll use plugins for the camera, preferences, and filesystem:

npm install @capacitor/camera @capacitor/preferences @capacitor/filesystem

Integrating PWA Elements (Optional)

If you plan to use Capacitor plugins, like the Camera API, within a web-based context (Progressive Web App), you may want to install Ionic’s PWA Elements:

npm install @ionic/pwa-elements

If you see this error: Unable to load PWA Element ‘pwa-camera-modal’.

// add these two line in main file where root id add on createRoot
import { defineCustomElements } from "@ionic/pwa-elements/loader";
defineCustomElements(window);

// like this code.
import React from "react";
import { createRoot } from "react-dom/client";
import { defineCustomElements } from "@ionic/pwa-elements/loader";
import App from "./App";

defineCustomElements(window);
const container = document.getElementById("root");
const root = createRoot(container!);
root.render(
   <App />
);

This step is optional and depends on your project requirements.

Building for Deployment

To prepare your app for deployment, you need to build it:

ionic build

Deploying on Mobile Devices

For deploying your app on mobile devices (iOS and Android), you’ll need to follow these steps:

iOS

  1. Add the iOS platform:
ionic cap add ios
  1. Copy your web assets to the native platform:
ionic cap copy ios
  1. If you’re using Xcode, you can open your project using:
ionic cap open ios

Android

  1. Add the Android platform:
ionic cap add android
  1. Copy your web assets to the native platform:
ionic cap copy android
  1. If you’re using Android Studio, open your project from there.

Synchronizing with Native Platforms

Whenever you make changes to your app that affect the native platform, you need to synchronize those changes:

For Android Studio users, run:

ionic cap sync android

For Xcode users, run:

ionic cap sync ios

Alternative: Live Reload

During development, you can use live reload for both web and native Android Studio development:

iOS

ionic cap run ios -l --external

Android

ionic cap run android -l --external

Generate icons and splash screens for iOS, Android, Progressive Web Apps

This @capacitor/assets generate tool is designed to automatically crop and resize JPEG and PNG source images, enabling the effortless creation of icons and splash screens for iOS, Android, and Progressive Web Apps through Capacitor integration.

The tool expects a assets or resources folder to exist in the root of the project.

npx @capacitor/assets generate --iconBackgroundColor '#eeeeee' --iconBackgroundColorDark '#222222' --splashBackgroundColor '#eeeeee' --splashBackgroundColorDark '#111111'

These commands will enable live reload for your Ionic React app on iOS and Android, allowing you to see changes in real-time as you develop.

With these steps, you’re all set to develop, build, and deploy your Ionic React app with Capacitor integration. Happy coding!

Ionic React app layout structure( Header, Footer, etc.) ref URL

Ionic React app default Components ref URL

Ionic React app Capacitor Plugins ref URL

Ionic React Capacitor Community refURL