2

我有像

创建日期=“2013-09-24 04:29:30”;

我必须在这个时间设置提醒。我正在使用本地通知来设置提醒,但我不知道如何在它发生后将其删除,以及如何在提醒时间两分钟后再次调用它。

提前致谢。

EKEventStore *eventStore = [[EKEventStore alloc]init];
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 =  datePicker.date; //set date
event.URL = [NSURL URLWithString:@"url"];

//for alert set the aleram and notify the user rest is taken care by calendar for u

   EKAlarm *alaram = [[EKAlarm alloc]init];
   [alaram setAbsoluteDate:datePicker.date];
   [event addAlarm:alaram];
//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");
4

2 回答 2

4

我正在发布与将事件添加到日历和设置剩余时间相关的代码希望你能有所了解


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

 }


于 2013-09-27T05:17:42.270 回答
1

这是工作代码..:)

  EKEventStore *eventStore = [[EKEventStore alloc] init] ;
  EKEvent *event = [EKEvent eventWithEventStore:eventStore];
   if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
    // iOS 6 and later
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,   NSError *error) {
        if (granted){
            //---- codes here when user allow your app to access theirs' calendar.
            [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 =  datePicker.date; //set date
            event.URL = [NSURL URLWithString:@"url"];
            NSDate *newDate = [datePicker.date dateByAddingTimeInterval:24.0f * 60.0f * 60.0f + 120.0f];
            event.endDate= newDate ;
            //for alert set the aleram and notify the user rest is taken care by calendar for u
            EKAlarm *alaram = [[EKAlarm alloc]init];

            [alaram setAbsoluteDate:datePicker.date];
            [event addAlarm:alaram];
            //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");
            }
        }else
        {
            //----- codes here when user NOT allow your app to access the calendar.
        }
    }];
}
else {
    //---- codes here for IOS < 6.0.
    //[self performCalendarActivity:eventStore];
}
于 2013-09-27T07:19:55.740 回答