How to check device is rooted or not in Ionic?

Share the post

Hello All, In this Ionic tutorial, We will learn How to check device is rooted or not in Ionic application with the help of a simple example. In the last Ionic tutorial, We had discussed How to check the battery status in the Ionic application. Now,

We are going to use a Cordova plugin cordova-plugin-iroot. Using this plugin we will check whether our android device is rooted or not.

If we talk about our application security then we must think about the device is rooted or not? Therefore, We need to check it as soon as our application gets installed on the user’s device.

So, let’s start.

Steps for How to check device is rooted or not in Ionic?

  • Step 1: Create a new Ionic application
  • Step 2: Install plugin
  • Step 3: Write the logic
  • Step 4: Run the application
  • Step 5: Output

Step 1: Create a new Ionic application

We will create a new ionic application to check whether the device is rooted or not? Use the following command to create a new ionic application

ionic start myApp blank

Open newly created project in Visual Studio Code using the following command

code .

Step 2: Install plugin

Now, We are going to use the cordova-plugin-iroot plugin. So, need to install it using the below command

ionic cordova plugin add cordova-plugin-iroot

Now, Install npm dependency using the below command

npm i cordova-plugin-iroot

Step 3: Write the logic

Now, We will write our logic inside our app.component.ts. because we need to check rooted functionality before our app executes. Before writing the rooted logic, We need to check the platform is ready or not. After the platform is ready we can write our code.

import { Component } from '@angular/core';
import { Platform } from '@ionic/angular';

declare var IRoot: any;

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})

export class AppComponent {

  constructor(private platform: Platform) {
    this.platform.ready().then(() => {
      this.checkIsRooted();
    })
  }

  checkIsRooted() {
    IRoot.isRooted(
      booleanValue => {
        console.log(booleanValue);
        if (booleanValue == true) {
          console.log("Device is rooted");
          alert("Device is rooted");
        } else {
          console.log("Device is not rooted");
          alert("Device is not rooted");
        }
      },
      err => {
        console.log(err);
        alert(err);
      }
    )
  }



}

Step 4: Run the application

Now, We have to check whether our logic is working or not. So, connect your device through USB & run the application using the following command

ionic cordova run android

Step 5: Output

Please refer to the below image for the output

How to check device is rooted or not in Ionic
Device rooted or not output

2 thoughts on “How to check device is rooted or not in Ionic?”

Leave a Comment