将事件添加到iPhone中的默认日历时会出错

时间:2013-03-21 06:43:27

标签: iphone ios objective-c calendar ekeventstore

现在我在iPhone上处理日历应用程序。我正在尝试将事件添加到iPhone中的默认日历。在这种情况下,添加事件已成功完成。但我无法在适当的开始时间添加事件。事件的开始日期完全正确,但在iPhone的默认日历中时间不同。我使用了以下代码。请帮助我..

NSLog(@"开始标签是:%@",startLabel.text); // 2013-03-28 12:03:41 PM

NSLog(@"end label is : %@",endLabel.text);//2013-03-28 01:03:41 PM


     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
     [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
     [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
     NSLog(@"description is : %@",[dateFormat description]);


     NSDate *sDate=[dateFormat dateFromString:startLabel.text];      
     NSDate *eDate = [dateFormat dateFromString:endLabel.text];
     NSLog(@"startDate is : %@",sDate);//2013-03-28 12:03:41 +0000
     NSLog(@"endDate is : %@",eDate);// 2013-03-28 13:03:41 +0000
     self.eventStore = [[EKEventStore alloc] init];
     EKEvent *myevent = [EKEvent eventWithEventStore:self.eventStore];
     myevent.title = titleFld.text;
     myevent.location  = locFld.text;
     myevent.startDate = sDate;
     myevent.endDate =  eDate;
     [myevent setCalendar:[eventStore defaultCalendarForNewEvents]];
     NSError *err;
     [self.eventStore saveEvent:myevent span:EKSpanThisEvent error:&err];

1 个答案:

答案 0 :(得分:3)

使用[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

而不是

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

希望这能解决你的问题。