EddyVerbruggen / Calendar-PhoneGap-Plugin

:date: Cordova plugin to Create, Change, Delete and Find Events in the native Calendar
773 stars 402 forks source link

The requestReadWritePermission not working #341

Open deepakrout opened 7 years ago

deepakrout commented 7 years ago

In our Ionic 2 app in one of the view user can add a particular event to the native Calendar. The issue is when initially it tries to access the ios calendar the user is prompted if they want to allow the app to access the native calendar. Here if the user selects No and later on they try to add an event to the calendar using apps 'Add Event to Calendar' button obviously they can't. However in the app we trying to request read/write permission by first checking Ionic 2 native if Calendar.hasWritePermission() if no then issues Calendar. requestReadWritePermission(). Here nothing happens. Looks like it neither resolves the promise nor rejects the promise. I am expecting in the success of the Calendar.requestReadWritePermission().then() it should bring up the dialog asking the user if they want to allow access to the calendar. Is this a bug or intended behavior? If this is intended then is there way to request permission in the app?

Calendar.requestReadPermission().then(data => { console.log('Request successed! ', data) //This line doesn't get executed }, err => { console.log('Request failed! ', err) //not this line }).catch(err => { console.log('Calendar request access failed.', err) //not even this line })

martijnlutgens commented 7 years ago

I use the calendar plugin in my ionic2 app too. The following works for me on Android. I haven't tested it on iOS. Note that it resolves a boolean and not data.

    Calendar.hasReadWritePermission().then((success) => {
        if (!success) {
            Calendar.requestReadWritePermission().then((success) => {
                if (!success) {
                    console.log('cannot request Calendar permissions');
                } else {
                    this.calendarOptions = Calendar.getCalendarOptions();
                }
            });
        } else {
            this.calendarOptions = Calendar.getCalendarOptions();
        }
    });
ursmeier-mercatus commented 7 years ago

Same issue: iOS does not work. Android works.

iOS: Once the user denied the permission there is no way to request the permission within the app.

Android:

Has anybody a solution for iOS other than telling the user to open the iOS settings?

chrisweight commented 7 years ago

I can confirm this is still an issue.

c0bra commented 7 years ago

I can't even get requestReadWritePermission to resolve its promise. I've tried manually changing the typscript defs in ionic and passing in a callback too and that doesn't work either.

chrisweight commented 7 years ago

Nope, I'm having the same issue too, it just doesn't want to resolve...combined with the other issues on the board, it's a problem...

chrisweight commented 7 years ago

Ok, did some digging the source code and for Android, you can actually request the thing you want to do directly and it should then automagically pop up the permission request alert first if you don't have permissions, meaning you DON'T need to request write permissions.

Sample code as follows:

chrisweight commented 7 years ago

`// HACK: on iOS, the 'hasWritePermission' call also triggers the Permission request alert, so we can just // call that return new Promise((resolve, reject) => { this.calendar .hasWritePermission() .then(hasPermission => { console.log('CalendarProvider.addEvent() -> hasReadWritePermission: ' + hasPermission);

      // HACK: If we're on Android, we try and create the item even if we don't have permission as THIS
      // will trigger the dialog
      if (hasPermission || this.isOnAndroid) {
        return this.createItem(item)
          .then(resolve)
          .catch(error => {

            // HACK: this is the pre-baked messaged that comes out of the plugin that we get on Android
            // when permission is turned down, so we intercept and use our own message.
            if (error === 'Please allow access to the Calendar and try again.') {
              reject(this.Messages.PERMISSION_DENIED);
              return;
            }

            reject(error);
          });
      }

      reject(this.Messages.PERMISSION_DENIED);
    })
    .catch(reject);
});`
adrigm commented 8 months ago

In IOS you need choose Full Access on Info.plist

Captura de pantalla 2024-01-30 a las 20 30 53