Xcode:事件未在日历中创建:问题与时区?

时间:2013-04-24 11:50:31

标签: ios ios6 calendar xcode4.5 eventkit

dd我在Native CALENDAR中创建事件时遇到问题,当我传递 event.startDate 除了当前时间戳以外的任何内容...还 event.endDate 不能使用不同的价值如下..

- (void)createEventInCalendar:(NSString *)calendarType withTitle:(NSString *)eventTitle startDate:(NSString *)stDate endDate:(NSString *)endDATE withNotes:(NSString *)paramNotes addAlarm:(BOOL)alarmNeeded {

EKEventStore *eventStore = [[EKEventStore alloc] init];

/* Are there any calendars available to the event store? */
if ([eventStore.calendars count] == 0){
    NSLog(@"No calendars are found.");
}


EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title     = [NSString stringWithFormat:@"%@ with Alarm", eventTitle];

//Convert String to DATE
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:SS"];

NSDate* date1 = [dateFormat dateFromString:stDate];
NSDate* date2 = [dateFormat dateFromString:endDATE];
//get Difference betwenn 2 DATE Values
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];

event.startDate = date1;
event.endDate   = [date1 initWithTimeInterval:distanceBetweenDates sinceDate:event.startDate];
event.notes = paramNotes;

/* The alarm goes off 2 seconds before the event happens */
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-2.0f];

[event addAlarm:alarm];

[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"Error shows: %@", err);

//Show EVENT EDIT Screen to User.
EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];

controller.eventStore       = eventStore;
controller.event            = event;
controller.editViewDelegate = self;

[self presentModalViewController:controller animated:YES];

}

调试后:Error Domain = EKErrorDomain Code = 4“开始日期必须在结束日期之前。” UserInfo = 0x223170 {NSLocalizedDescription =开始日期必须在结束日期之前。}

作为我的日志:ST日期:2013-04-25 07:50:00            &安培;结束日期:2013-04-29 07:50:00

但EVENT EDIT视图在START DATE之前的某个时间显示END DATE值。它很奇怪。

任何人都可以指导我如何解决它吗? 感谢。

1 个答案:

答案 0 :(得分:1)

问题已解决:

一个简单的错误:

//get Difference betwenn 2 DATE Values
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];

应该是

//get Difference betwenn 2 DATE Values
NSTimeInterval distanceBetweenDates = [date2 timeIntervalSinceDate:date1];

感谢。