5

I am attempting to save an event into the calendar, from my application. My code works for iOS 7, but on iOS 6, it returns No calendar has been set.

The application prompts for user to grant access to the calendar, on iOS 7. But no such prompt appears for iOS 6. Although the application is granted access in the Settings-> Privacy -> Calendar.

And yes, I have already implemented the requestAccessToEntityType:completion:.

Here is my code snippet.

EKEventStore *objStore = [[EKEventStore alloc]init];

if ([objStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    // iOS 6 and later
    [objStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (granted)
        {
            // code here for when the user allows your app to access the calendar
            EKEvent *calEvent = [EKEvent eventWithEventStore:objStore];
            calEvent.title = mstrTitleEvent;
            calEvent.startDate = self.dateToBeSet;
            calEvent.endDate = self.dateToBeSet;
            calEvent.calendar = objStore.defaultCalendarForNewEvents;

            EKAlarm *objAlarm = [EKAlarm alarmWithAbsoluteDate:self.dateToBeSet];
            [calEvent addAlarm:objAlarm];

            NSError *error;
            BOOL _bStatus = [objStore saveEvent:calEvent span:EKSpanThisEvent commit:YES error:&error];

            UIAlertView *alertV;

            if(_bStatus)
            {
                alertV = [[UIAlertView alloc]initWithTitle:@"Congratulations" message:@"Saved To Calendar" delegate:nil cancelButtonTitle:@"Right On!" otherButtonTitles:nil];
                [alertV show];
            }
            else
            {
                alertV = [[UIAlertView alloc]initWithTitle:@"Alert" message:[NSString stringWithFormat:@"Error saving to calendar, with error %@.",[error localizedDescription]] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
                [alertV show];
            }
        }
            else
        {
            // code here for when the user does NOT allow your app to access the calendar
            UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please grant access to the calendar, and try again later." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alertV show];
        }
        });
    }];

}

Hitesh
  • 542
  • 3
  • 12
  • Did you checked the `error` parameter (first one). In which line does it don't go? – Larme May 19 '14 at 09:49
  • Yes. Its going inside the requestAccess method. And hence is displaying the error in the alert message. the bool variable _bStatus comes out to false. – Hitesh May 19 '14 at 09:53
  • EKSpanThisEvent tells the calendar, that any changes made to the event will reflect only to the current event. And not to the future events, if recurring. – Hitesh May 20 '14 at 11:38

2 Answers2

1

Just managed to somehow find a solution for my problem. I had to navigate from one page to another, so posting the link to the two pages.

First -> https://discussions.apple.com/message/16497282#16497282

Then, from there to -> https://discussions.apple.com/message/16479587#16479587

I had to go into Settings>iCloud> and turn on Calendars. After that, I tried to attempt and run my code, and it was working well and fine again.

Do attempt this, if you facing a similar problem.

I was working on iPad 2, and with iOS 6.1.3 installed on the device.

Hitesh
  • 542
  • 3
  • 12
  • This can't possibly be a solution when my legacy app can still save event successfully, but our upgraded one throws this error. – Daniel Oct 23 '19 at 08:09
0

Amazing I am testing my code on two different devices, one it works fine and the other it will not work. Just looked in setting and the one with it not working has iCloud calendar and reminders turned off, just turn them on and it all works now... this has to be a bug

Faithjda
  • 1
  • 1