KDM
KDM

Reputation: 197

iOS EKEventStore Not saving Calendars

I can't seem to figure out why the following code is not working. I get the NSlogs for "Calendar Created" and "Calendar Existed" I also get the Calendar ID without issue. I have had a bunch (like 10) of the created calendars just "Appear" at one point but I deleted them all the kept trying and they are not showing up. I'm really confused as to why it's not working.

-(void)saveEventWithDate:(NSDate *)startDate endDate:(NSDate *)endDate {
    AppData *theData = [self theAppData];

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

    if([self checkIsDeviceVersionHigherThanRequiredVersion:@"6.0"]) {
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            if (granted){

                EKEvent *event  = [EKEvent eventWithEventStore:eventStore];

                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

                if ([defaults objectForKey:@"Calendar"] == nil) // Create Calendar if Needed
                {
                    EKSource *theSource = nil;

                    for (EKSource *source in eventStore.sources) {
                        if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {
                            theSource = source;
                            NSLog(@"iCloud Store Source");
                            break;
                        } else {
                            for (EKSource *source in eventStore.sources) {
                                if (source.sourceType == EKSourceTypeLocal) {
                                    theSource = source;
                                    NSLog(@"iPhone Local Store Source");
                                    break;
                                }
                            }
                        }
                    }

                    EKCalendar *cal;
                    cal = [EKCalendar calendarWithEventStore:eventStore];
                    cal.title = @"hello";
                    cal.source = theSource;
                    [eventStore saveCalendar:cal commit:YES error:nil];
                    NSLog(@"cal id = %@", cal.calendarIdentifier);
                    NSString *calendar_id = cal.calendarIdentifier;
                    [defaults setObject:calendar_id forKey:@"Calendar"];
                    event.calendar  = cal;

                } else {
                    event.calendar  = [eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]];
                    NSLog(@"Calendar Existed");
                }

                event.title     = [NSString stringWithFormat:@"%@ iPhone",[theData.repair_info objectForKey:@"name"]];
                event.location  = @"Location of";
                event.notes     = @"Notes";
                event.startDate = startDate;
                event.endDate   = endDate;
                event.allDay    = NO;
                EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-1800]; // Half Hour Before
                event.alarms = [NSArray arrayWithObject:alarm];

                [eventStore saveEvent:event span:EKSpanThisEvent error:nil];

            } else {
                NSLog(@"Not Granted");
            }

        }];
    } }

Upvotes: 1

Views: 5231

Answers (3)

nghien_rbc
nghien_rbc

Reputation: 145

I thinks you don't need to have a "Reset Calendar" button under settings if the user deleted a calendar. Just change a code row from:

if ([defaults objectForKey:@"Calendar"] == nil) // Create Calendar if Needed

to

if ([defaults objectForKey:@"Calendar"] == nil || ![eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]]) // Create Calendar if Needed

and you will create a new calendar and add new event to it

Upvotes: 2

Jazz Madahar
Jazz Madahar

Reputation: 41

The problem is in the very first line of the code.

//Wrong Way..
EKEventStore *eventStore = [[EKEventStore alloc] init];

You have made another instance of EKEventStore and you want to do transactions in the existing instance.

Instead of "alloc init" it here, fetch your EKEventStore instance from the class which is managing your datasource for calendar. which may look like as.

EKEventStore *eventStore = [dataSource getEventSore];

Also instead of keeping track of calendar's ID. I would suggest you to keep track of EKEvent's identifier.

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
NSLog(@"eventIdentifier = %@",event.eventIdentifier);

Upvotes: 1

KDM
KDM

Reputation: 197

The issue was the the user defaults were set to the very first calendar created (then deleted) so it would not create another one. I had to clear my NSUserDefaults and the calendars got created. So I will have to have a "Reset Calendar" button under settings if the user deleted a calendar.

Upvotes: 0

Related Questions