I want to add event for 55 year from current date.Using below code for iOS 6,event is added in calendar with event name "New Event" and event was set to all day.This is not correct as per my code.Code is working perfectly for events which are below 55 year from current date.
-(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(@"==================================");
}
screenshot of calendar app after adding events programmatically:
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] ==================================