Applying cordova-plugin-android-permissions for ionic 2

Hi sorry for newbie question. I want to use this plugin on my ionic 2 project. I tried to convert sample code to angular 2. Since I’m using .then() as a substitute for callback I always got error “Cannot read property ‘then’ of undefined”. I want to know if it is possible to change callback to .then() promises. If possible I want to know why I already tried this code:

let permissions = cordova.plugins.permissions;
permissions.hasPermission(permissions.READ_EXTERNAL_STORAGE)
.then((status:any) => console.log(‘status.hasPermission’));

plugin source: https://www.npmjs.com/package/cordova-plugin-android-permissions

Sorry for my question. I’d already figure it out. I just need to convert callback to promises before I can implement the .then() function.

Can you please tell me how you did this? I am having problem in using this in my IONIC 2 project.
Thanks in advance.

The plugin of cordova for android permission didn’t implement the Promises. If you want to use .then() function you must read this http://learnangular2.com/es6/promises.

@JessMark Do you have any code example where you could achieve this? It will be of great use.

This is how I do it.

addPermission(androidPermission: any) {
    return new Promise((resolve, reject) => {
      let permissions = cordova.plugins.permissions;
      permissions.requestPermission(androidPermission, function(status) {
    if (status.hasPermission){
      resolve("Permission is now granted");
    } else{
      reject('Permission is not turned on');
    }
  }, reject('Permission is not turned on'));

});

}

Thank you :slight_smile: Saved my day!