在我的iOS应用程序中,我想在日历中创建事件,我已经找到了代码,但代码直接创建一个事件,而不是打开添加事件屏幕。我想让用户通过添加事件屏幕设置提醒。
我的代码在下面:
EKEventStore *es = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:es];
event.title = @"Event Title";
event.startDate = [NSDate date]; // today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; // Duration 1 hr
[event setCalendar:[es defaultCalendarForNewEvents]];
NSError *err = nil;
[es saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSLog(@"Error : %@", err);
} else {
// Denied
}
}];
} else {
BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized);
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:es];
event.title = @"Event Title";
event.startDate = [NSDate date]; // today
event.endDate = [[NSDate date] dateByAddingTimeInterval:60*60]; // Duration 1 hr
[event setCalendar:[es defaultCalendarForNewEvents]];
NSError *err = nil;
[es saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSLog(@"Error : %@", err);
} else {
// Denied
}
}
根据EKEventStore文档,方法:[es saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
正在保存事件。你的代码工作正常,这段代码不应该像你期望的那样打开"添加事件屏幕"。