select日历以添加iOS事件
我试图添加事件到iOS设备的日历,现在我必须添加一个事件到defaultCalendarForNewEvents
日历,但我想要的是能够select一个已经创build了我想要的日历添加事件。
例如在下面的捕获中,可以看到有一个iphone日历,然后可能会创build一个或几个gmail
我想要做的是您可以select要添加事件的日历,此日历必须先创build,而不是系统select日历。
任何build议? 任何例子?
PD:我开始用objective-c和ios编码
代码来添加事件我正在testing,它的工作原理:
- (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]; }
首先,感谢@NarendraPandey和@nehamishra提供的帮助
我已经给了我的问题的解决scheme,然后将解决scheme,以防有人可以为您服务。
我已经创build了一个获取本地和gmail的可用日历的方法,代码如下:
- (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; }
然后,为了显示日历列表,以便用户可以select一个,并在那里input事件,我发现我要使用一个Action Sheet
,虽然我已经看到,根据一些论坛的评论StackOverflow已弃用,所以我用UIAlertController
如下:
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"); }
凡[self downloadCalendario: cal];
函数负责从Web服务中下载一些事件并将其添加到所选日历中。
导致在以下视图中select日历:
而将事件添加到所选日历的代码是:
-(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]; }
我希望它能帮助别人。
尝试通过创build新的日历。
-(EKEvent*)createEvent:(EKEventStore*)eventStore{ EKEvent *event = [EKEvent eventWithEventStore:eventStore]; event.title = @"New Event"; event.startDate = [NSDate date]; event.endDate =[NSDate date]; event.location=@"Location"; event.allDay = YES; event.notes =@"Event description"; NSString* calendarName = @"Calendar"; EKCalendar* calendar; EKSource* localSource; for (EKSource *source in eventStore.sources){ if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]){ localSource = source; break; } } if (localSource == nil){ for (EKSource *source in eventStore.sources){ if (source.sourceType == EKSourceTypeLocal){ localSource = source; break; } } } calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore]; calendar.source = localSource; calendar.title = calendarName; NSError* error; [eventStore saveCalendar:calendar commit:YES error:&error]; return event; }
希望它能工作。 让我知道你是否有任何疑问。
-(void)addEventOnCalendar{ EKEventStore *store = [EKEventStore new]; [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { if (!granted) { return; } EKEvent *event = [EKEvent eventWithEventStore:store]; event.title = @"What ever You want to save as your event"; event.startDate = selectedDate; event.endDate = selectedDate1; event.calendar = [store defaultCalendarForNewEvents]; NSError *err = nil; [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err]; }]; UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:@"Event Successfully added in Calender" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [alert show]; }