我相信这是有效的提前一个小时将其添加到我的日历中。
我在另一个在CST中具有事件的应用程序上执行此操作,当我将它们添加到日历中时,将它们添加得很好。我的代码错误还是什么?
- (void)viewWillAppear:(BOOL)animated {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:self.event.timeZone]];
[dateFormat setDateFormat:@"yyyy-MM-ddHH:mm:ss"];
self.startDate = [dateFormat dateFromString:self.game.dateTime];
self.endDate = [[NSDate alloc] initWithTimeInterval:60*self.event.gameDuration sinceDate:self.startDate];
}
- (void) addToCalendar
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]){
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
for (EKSource *source in eventStore.sources)
{
if (source.sourceType == EKSourceTypeCalDAV || source.sourceType == EKSourceTypeLocal)
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:@"%@ vs. %@", self.game.awayTeam.team.name, self.game.homeTeam.team.name];
event.location = [NSString stringWithFormat:@"%@ - %@", self.game.venue.name, self.game.venue.court];
event.startDate = self.startDate;
event.endDate = self.endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(err) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlert:@"Error" desc:@"There was a problem adding this game to your calendar"];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self showAlert:@"Success" desc:@"Game added to your calendar"];
});
}
break;
}
}
}];
}
}
我发现Apple使用了自己的时区字符串,因此我在通过我们的API发送时,我只是从Windows TimeZones转换为Apple的翻译。