I'm trying to add events to the calendar in iOS devices, for now I've got to add an event to the defaultCalendarForNewEvents
calendar, but what I want is to be able to choose a calendar that has already been created in which I want to add the event.
For example in the following capture it is seen that there is an iphone calendar and then there may be 1 or several gmail created
What I want to do is that you can choose the calendar in which you want to add the event, this calendar must be created earlier, not that the system chooses the calendar.
Any recommendations? Any examples?
P.D: I'm starting to code with objective-c and ios
Code to add events I'm testing and it works:
- (void) addEventCalendar: (Evento_DTO *) evento {
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
event.title = @"Test Event";
reminder.title = @"Test reminder";
NSDate *cDate = [NSDate date];
NSLog(@"current date %@", cDate);
/*NSDateComponents *startDateComponents = [[[NSDateComponents alloc] init] autorelease];
[startDateComponents setDay:12];
[startDateComponents setMonth:12];
[startDateComponents setYear:2012];
[startDateComponents setHour:12];
[startDateComponents setMinute:18];
NSDateComponents *endDateComponents = [[[NSDateComponents alloc] init] autorelease];
[endDateComponents setDay:12];
[endDateComponents setMonth:12];
[endDateComponents setYear:2012];
[endDateComponents setHour:12];
[endDateComponents setMinute:18];
[endDateComponents setSecond:20];*/
//event.startDate = cDate;
//event.endDate = [cDate dateByAddingTimeInterval:15.0];
event.startDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];
event.endDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 30)];
//event.startDate = [[NSCalendar currentCalendar] dateFromComponents:startDateComponents];
//event.endDate = [[NSCalendar currentCalendar] dateFromComponents:endDateComponents];
reminder.completionDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 10)];
NSLog(@"startdate %@", event.startDate);
NSLog(@"enddate %@", event.endDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
//[reminder setCalendar:[eventStore defaultCalendarForNewReminders]];
NSError *error = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
//[eventStore saveReminder:reminder commit:YES error:&error];
}
First of all, thank @NarendraPandey and @nehamishra for the help provided
I have given the solution to my problem, which will then put the solution in case someone can serve you.
I have created a method to obtain the available calendars that are local and those of gmail, the code is the following one:
Then to show the list of calendars so that the user can select one and enter the events there, I found that Ihave to use an
Action Sheet
, although I have seen that it was deprecated according to some forum comments Of StackOverflow, so I usedUIAlertController
, being as follows:Where the
[self downloadCalendario: cal];
function is responsible for downloading some events from a web service and adding them to the chosen calendar.Resulting in the following view to choose the calendar:
And the code to add the event to the selected calendar is:
I hope it helps somebody.
Try this by creating new calendar.
Hope it will work. Let me know if you have any query.