在我的应用程序中,我需要将事件保存到设备日历中。目前,它确实保存在 iPad 日历中,但没有保存在 iPhone 日历中,这很奇怪。
这是我的代码:
-(IBAction)addToCalendar:(id)sender
{
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = titleString;
NSString *dateString = eventStartDate;
NSString *endString = eventEndDate;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSDate *dateToString = [dateFormatter dateFromString:endString];
// voila!
// dateFromString = [dateFormatter dateFromString:dateString];
// dateToString = [dateFormatter dateFromString:endString];
//(@"date %@ %@", dateFromString, dateToString);
event.startDate = dateFromString;
event.endDate = dateToString;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
BOOL saved = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
//(@"err %@", err);
if (saved) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"BHCS" message:@"Event saved to calendar." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
我该如何解决这个问题?