我有应用程序需要将事件写入本机日历。如果添加EKEvent并不包含EKAlarm,则写入工作正常。如果我包含这行代码:
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-30];
event.alarms = [NSArray arrayWithObject:alarm];
未添加日历项目。似乎根本不执行这行代码:
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
想法可能是什么问题?
答案 0 :(得分:5)
我运行了你的代码(并添加了使其完成所需的内容)并且没有任何问题。许多难以解释的案例的解决方案是清理(构建文件夹)并从设备重置模拟器/删除应用程序。如果您提供更多代码,则更容易找到解决方案。顺便说一句,这是我用过的代码:
- (void)addToCalendar {
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-30];
event.alarms = [NSArray arrayWithObject:alarm];
event.title = @"Title";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
event.startDate = [df dateFromString:@"2014-02-05 10:00:00"];
event.endDate = [event.startDate dateByAddingTimeInterval:90*60];
event.notes = @"Add some notes";
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}];
}