更新时间:2023-12-03 14:58:52
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:
- (NSMutableArray*) getCalendars {
NSMutableArray *res =[[NSMutableArray alloc] init];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEntityType type = EKEntityTypeEvent;
NSArray *calendars = [eventStore calendarsForEntityType: type];
for ( EKCalendar *cal in calendars )
{
if (cal.type == EKCalendarTypeCalDAV || cal.type == EKCalendarTypeLocal ){
NSLog(@"cal nombre:- %@ ", cal.title);
[res addObject: cal];
}
}
return res;
}
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 ***, so I used UIAlertController
, being as follows:
NSMutableArray* cals = [self getCalendars];
if([cals count] > 0){//Comprobamos que existan calendarios
UIAlertController *alert = [UIAlertController alertControllerWithTitle:AMLocalizedString(@"calendar_dialog_info", @"")
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for ( EKCalendar *cal in cals )
{
UIAlertAction *calAction = [UIAlertAction actionWithTitle: cal.title
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(@"You pressed button %@ ", cal.title);
[self descargarCalendario: cal];
}];
[alert addAction:calAction];
}
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:AMLocalizedString(@"cancelar", @"")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}else{
NSLog(@"No hay calendarios");
}
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:
-(void)addEventOnCalendar: (EKCalendar *) cal{
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) {
return;
}
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Test";
NSDate *cDate = [NSDate date];
event.startDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];
event.endDate = [cDate dateByAddingTimeInterval:((5*60*60) + (30 * 60) + 15)];
//event.calendar = [store defaultCalendarForNewEvents];
event.calendar = [store calendarWithIdentifier: cal.calendarIdentifier];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Event Successfully added " delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
}
I hope it helps somebody.