Arijit
Arijit

Reputation: 1674

Ionic cordova-plugin-android-permissions not showing while installation

I have followed the official documentation from here. I have updated my app.component.ts like below:

constructor(private androidPermissions: AndroidPermissions) {

platform.ready().then(() => {
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.CAMERA, this.androidPermissions.PERMISSION.GET_ACCOUNTS]);

.....
});

}

This is working as expected.

Now the problem is, the app is asking permission after installation (Not while installing). First time when the app is loaded after accepting permission it is not able to access camera. Now if I close the app and open again, its able to access the camera.

So the summery is: The app is not able to access the camera when first time launched. From second time onward its working perfectly.

As per my understanding, My home page is loading before the app.component.ts executes fully. I have to hold the home page loading till user accepts the permission. Or this can be solved if the app asks permission while installation. But I am unable to implement any of the above solution. Please help.

Upvotes: 0

Views: 3888

Answers (2)

Stefan
Stefan

Reputation: 1253

Certain permissions in Android require a runtime check, such as the camera permission. Before Android 6 it was only necessary to add permissions to the manifest, however since Android 6 you must verify a permission at runtime.

A user can at any point revoke a permission (see android app settings). You can at no point rely on a permission. Before your app uses the camera a checkPermission call is obligatory!

Upvotes: 2

Shadi Shaaban
Shadi Shaaban

Reputation: 1720

androidPermissions.requestPermissions should return a promise, try putting your code after the promise resolves as follows:

platform.ready().then(() => {
this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.CAMERA, this.androidPermissions.PERMISSION.GET_ACCOUNTS]).then(
      result => {
          console.log('User allowed access to camera');
          // put your code here
          alert("Camera Enabled!");
      },
      err => { console.error('User denied access to camera!'); }
    );
});

Upvotes: -1

Related Questions