0
 EKEventStore *eventDB = [[EKEventStore alloc] init];
 EKEvent *myEvent  = [EKEvent eventWithEventStore:eventDB];
 NSString *trimmedString = [[arrDateList objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 myEvent.title     = [d valueForKey:@"garden_name"];
 myEvent.startDate = [dateFormatter dateFromString:trimmedString];
 myEvent.endDate   = [dateFormatter dateFromString:trimmedString];
 myEvent.allDay = TRUE;

 [myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
 NSError *err;
 [eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];

我正在使用上面的代码行,但我无法将事件存储在日历中......请帮助我在代码行中有错误的地方......谢谢并问候 Rahul Virja

4

1 回答 1

0

你可以试试这个代码..

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm a"]; NSDate *date = [dateFormatter dateFromString:[taskDetails taskStartDateTime]];

如果调试

    NSLog(@"here is the task start %@",[taskDetails taskStartDateTime]);

万一

    NSDate *end = [dateFormatter dateFromString:[taskDetails taskEndDateTime]];
    [dateFormatter release];
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title = [taskDetails taskGottaDo];   
    event.startDate = date;
    event.endDate = end;
    EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-6*60*60];
    [event addAlarm:reminder];
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    NSTimeInterval alarmOffset = -1*60;//1 hour
    EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];

    [event addAlarm:alarm];
    [eventStore release];

    NSError *err;
    BOOL saved = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

   // NSLog(@"here is the error %@",[eventStore saveEvent:event span:EKSpanThisEvent error:&err]);
    if (saved == YES)
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@""
                                  message:@"Saved to calendar"
                                  delegate:nil
                                  cancelButtonTitle:@"Right On!" otherButtonTitles:nil] ;
        [alertView show];   
        [alertView release];
    }
于 2012-07-31T14:33:56.690 回答