简体   繁体   中英

iOS: Open a Calendar app New Event screen with populated date

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.

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM