我想从当前日期添加 55 年的事件。使用下面的 iOS 6 代码,事件添加到日历中,事件名称为“新事件”,事件设置为全天。根据我的代码,这是不正确的。代码是非常适合从当前日期起 55 年以下的事件。
-(void)addEvent{
EKEventStore *es = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
[self loadAllEventsForStore:es];
} else {
NSLog(@"Not Granted");
}
}];
} else {
BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized);
if (granted) {
[self loadAllEventsForStore:es];
} else {
NSLog(@"Not Granted");
}
}
}
-(void)loadAllEventsForStore:(EKEventStore*)store{
[self setEventForStore:store yearfromCurrentDate:55];
[self setEventForStore:store yearfromCurrentDate:56];
}
-(void)setEventForStore:(EKEventStore*)store yearfromCurrentDate:(int)year{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = [NSString stringWithFormat:@"Event %d",eventid++];
NSLog(@"=========== %@ ==============", event.title);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
[comps setDay:05];
[comps setMonth:1];
[comps setYear:year];
NSDate *startDate = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *startDateString = [dateFormatter stringFromDate:startDate];
NSLog(@"Start Date = %@", startDateString);
event.startDate = startDate;
NSDate *endDate = [event.startDate dateByAddingTimeInterval:60*60];;
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *endDateString = [dateFormatter stringFromDate:endDate];
NSLog(@"End Date date = %@", endDateString);
event.endDate = endDate;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
BOOL save=[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
if (save && err==nil) {
NSLog(@"Event %@ is added sucessfully",event.title);
}else
NSLog(@"Event %@ is not added",event.title);
NSLog(@"==================================");
}
以编程方式添加事件后日历应用程序的屏幕截图:
Console Output:
2014-01-22 15:13:08.789 TestApp[19094:907] =========== Event 0 ==============
2014-01-22 15:13:08.796 TestApp[19094:907] Start Date = 27-02-2069
2014-01-22 15:13:08.798 TestApp[19094:907] End Date date = 27-02-2069
2014-01-22 15:13:08.884 TestApp[19094:907] Event Event 0 is added sucessfully
2014-01-22 15:13:08.886 TestApp[19094:907] ==================================
2014-01-22 15:13:08.889 TestApp[19094:907] =========== Event 1 ==============
2014-01-22 15:13:08.893 TestApp[19094:907] Start Date = 27-02-2070
2014-01-22 15:13:08.896 TestApp[19094:907] End Date date = 27-02-2070
2014-01-22 15:13:08.923 TestApp[19094:907] Event Event 1 is added sucessfully
2014-01-22 15:13:08.924 TestApp[19094:907] ==================================