我正在发布与将事件添加到日历和设置剩余时间相关的代码希望你能有所了解
- (void)addEventToCalendar
{
EKEventStore *eventStore = [[EKEventStore alloc]init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//for IOS > 6.0
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
//no need to fill all fill which one u want to set
event.title =@"eventTitle";
event.location = @"eventLocation";
event.notes = @eventNote";
event.startDate = eventStartDate; //set date
event.endDate = eventEndTime;
event.URL = [NSURL URLWithString:@"url"];
//for alert set the aleram and notify the user rest is taken care by calendar for u
switch (evetReminder) { //set alaram for 5mins, 15mins ,20mins etc
case 0:
self.selectedAlertSetting = @"None";
break;
case 1:
{
EKAlarm *alaram = [[EKAlarm alloc]init];
[alaram setAbsoluteDate:eventStartDate];
[event addAlarm:alaram];
[alaram release];
break;
}
case 2:
{
NSTimeInterval aInterval = -5 *60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 3:
{
NSTimeInterval aInterval = -15 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 4:
{
NSTimeInterval aInterval = -30 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 5:
{
NSTimeInterval aInterval = -1 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 6:
{
NSTimeInterval aInterval = -2 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 7:
{
NSTimeInterval aInterval = -1 * 24 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 8:
{
NSTimeInterval aInterval = -2 * 24 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
default:
break;
}
//finally add it to calendar
NSError *err = nil;
BOOL complete = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(err)
{
NSLog(@"error in storing event");
}
else
NSLog(@"successfully added");
if(complete)
{
NSLog(@"successfully added");
}
else
{
NSLog(@"error in storing event");
}
[eventStore release];
}
}];
}
else
{
//for IOS < 6.0
// perform same action hear
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
//no need to fill all fill which one u want to set
event.title =@"eventTitle";
event.location = @"eventLocation";
event.notes = @eventNote";
event.startDate = eventStartDate; //set date
event.endDate = eventEndTime;
event.URL = [NSURL URLWithString:@"url"];
//for alert set the aleram and notify the user rest is taken care by calendar for u
switch (evetReminder) { //set alaram for 5mins, 15mins ,20mins etc
case 0:
self.selectedAlertSetting = @"None";
break;
case 1:
{
EKAlarm *alaram = [[EKAlarm alloc]init];
[alaram setAbsoluteDate:eventStartDate];
[event addAlarm:alaram];
[alaram release];
break;
}
case 2:
{
NSTimeInterval aInterval = -5 *60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 3:
{
NSTimeInterval aInterval = -15 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 4:
{
NSTimeInterval aInterval = -30 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 5:
{
NSTimeInterval aInterval = -1 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 6:
{
NSTimeInterval aInterval = -2 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 7:
{
NSTimeInterval aInterval = -1 * 24 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
case 8:
{
NSTimeInterval aInterval = -2 * 24 * 60 * 60;
EKAlarm *alaram = [EKAlarm alarmWithRelativeOffset:aInterval];
[event addAlarm:alaram];
break;
}
default:
break;
}
//finally add it to calendar
NSError *err = nil;
BOOL complete = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(err)
{
NSLog(@"error in storing event");
}
else
NSLog(@"successfully added");
if(complete)
{
NSLog(@"successfully added");
}
else
{
NSLog(@"error in storing event");
}
[eventStore release];
}
}