iOS:打开日历应用程序新建事件屏幕,其中包含已填充的日期

问题描述 投票:2回答:1

是否可以通过编程的开始和结束日期以编程方式将用户重定向到日历应用程序新事件屏幕?我知道Introduction to Calendars and Reminders,但这似乎是一种矫枉过正。我也尝试了calshow://,但似乎没有工作,或者我找不到正确的方案。

ios objective-c iphone swift calendar
1个回答
3
投票
@import EventKit;
@import EventKitUI;

然后使用这个呈现eventkit:

- (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;
            }
    }];
  }
}

同时,有一些代表提供了日历结束日期开始日期的详细信息。

#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;
}

此createEvent将创建新日历

如果您还有其他问题,请与我们联系。

© www.soinside.com 2019 - 2024. All rights reserved.