Is it possible to redirect a user to Calendar app New Event screen programmatically with populated start and end dates? I am aware of Introduction to Calendars and Reminders, but that seems to be an overkill. I have also tried calshow://, but didn't seem to work either or I am couldn't find a correct scheme.
Asked
Active
Viewed 1,760 times
1
romants
- 3,482
- 1
- 20
- 30
-
1yes this can happen you can redirect it. – Narendra Pandey Nov 10 '16 at 10:28
-
@NarendraPandey would you mind elaborating how? – romants Nov 10 '16 at 10:29
1 Answers
3
@import EventKit;
@import EventKitUI;
then present eventkit using this:
- (IBAction)ScheduleClicked:(id)sender {
EKEventStore *eventStore = [[EKEventStore alloc]init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,NSError* error){
if(!granted){
NSString *message = @"Hey! This Project Can't access your Calendar... check your privacy settings to let it in!";
dispatch_async(dispatch_get_main_queue(), ^{
// Present alert for warning.
});
}else{
EKEventEditViewController *addController = [[EKEventEditViewController alloc] initWithNibName:nil bundle:nil];
addController.event = [self createEvent:eventStore];
addController.eventStore = eventStore;
[self presentViewController:addController animated:YES completion:nil];
addController.editViewDelegate = self;
}
}];
}
}
Meanwhile there are some delegates for giving detail of end dates start date of calendar.
#pragma mark - eventEditDelegates -
- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action{
if (action ==EKEventEditViewActionCanceled) {
[self dismissViewControllerAnimated:YES completion:nil];
}
if (action==EKEventEditViewActionSaved) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - createEvent -
-(EKEvent*)createEvent:(EKEventStore*)eventStore{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"New Event";
event.startDate = @"Your start date";
event.endDate = @"Your end 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;
}
This createEvent will create new calendar
Let me know if you have any more questions.
marc_s
- 704,970
- 168
- 1,303
- 1,425
Narendra Pandey
- 672
- 9
- 28
-
I stated that I don't care about creating event programmatically, just about redirecting to Calendar App New Event screen with pre-populated values – romants Nov 10 '16 at 10:39
-
you can redirect EKEventEditViewController that is your event screen – Narendra Pandey Nov 10 '16 at 10:39
-
and same thing i have done check above code. you can enter Event screen with pre-populated values in this delegates -(EKEvent*)createEvent:(EKEventStore*)eventStore{ – Narendra Pandey Nov 10 '16 at 10:40
-
I am confused, why does you code call `eventStore saveCalendar:calendar commit:YES` then? – romants Nov 10 '16 at 10:44
-
basically you have to create calendar. if you not then you can use your default calendar. by the way if you want your project calendar then you can do this. – Narendra Pandey Nov 10 '16 at 10:46
-
@NarendraPandey sir could you please help us to answer this question ? it is similar but I need it in Swift https://stackoverflow.com/questions/61677145/how-to-open-create-event-screen-in-calendar-app-with-populated-data-in-swift – Alexa289 May 08 '20 at 10:34