In our daily life we need to set alert to remember ourself for specific task.In ios application using eventStore we can create events and set alert to it.
First we need to add EventKit Framework in application
First we need to add EventKit Framework in application
- Here in our application we need to display event name listing if perticuler event not existing in iCalender then displayed enabled calender icon and if already exists then disabled calender icon is displyed
- when user tap on calender button that event should be added in iCalender with alert
First define this variable in .h file
EKEventStore *eventStore;
then in your viewDidLoad method check eventstore class is available or not.
EKEventStore *eventStore;
then in your viewDidLoad method check eventstore class is available or not.
- (void)viewDidLoad
{
[super viewDidLoad];
eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
}
else if (!granted)
{
// display access denied error message here
}
else
{
// access granted
// ***** do the important stuff here *****
}
});
}];
}
else
{
}
}
{
[super viewDidLoad];
eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
}
else if (!granted)
{
// display access denied error message here
}
else
{
// access granted
// ***** do the important stuff here *****
}
});
}];
}
else
{
}
}
on calender button click check if event already exists or not and then if not add in iCalender.
first check event in Calender use this code
-(BOOL)checkifAppointmentExists:(NSDate *)startDate withEndDate:(NSDate *)endDate withTitle:(NSString*)str{
BOOL addinAppointment = FALSE;
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:nil];
// Fetch all events that match the predicate
NSArray *eventsArray = [eventStore eventsMatchingPredicate:predicate];
if([eventsArray count]>0)
{
if([eventsArray objectAtIndex:0])
{
for (EKEvent *event in eventsArray) {
NSString *strId = [[NSString alloc] initWithFormat:@"%@", event.title];
if([strId isEqualToString:str])
addinAppointment = TRUE;
}
}
}
return addinAppointment;
}
BOOL addinAppointment = FALSE;
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:nil];
// Fetch all events that match the predicate
NSArray *eventsArray = [eventStore eventsMatchingPredicate:predicate];
if([eventsArray count]>0)
{
if([eventsArray objectAtIndex:0])
{
for (EKEvent *event in eventsArray) {
NSString *strId = [[NSString alloc] initWithFormat:@"%@", event.title];
if([strId isEqualToString:str])
addinAppointment = TRUE;
}
}
}
return addinAppointment;
}
-(void)addIniCalender{
int clickedTag = alertView.tag;
NSString *alarmDate = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"appointmentdate"];
NSString *startTime = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"starttime"];
startTime = [NSString stringWithFormat:@"%@ %@",alarmDate,startTime];
NSString *endTime = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"endtime"];
NSString *title = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"address"];
endTime = [NSString stringWithFormat:@"%@ %@ ",alarmDate,endTime];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
NSDateFormatter *df= [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy HH:mm"];
NSDate *FireDate = [df dateFromString:startTime];
NSDate *EndDate = [df dateFromString:endTime];
if([self checkifAppointmentExists:FireDate withEndDate:EndDate withTitle:title]== TRUE)
{
}
else{
event.startDate = FireDate;
event.endDate = EndDate;
event.allDay = YES;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSArray *arrAlarm = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:FireDate]];
event.alarms= arrAlarm;
NSError *err;
if ([eventStore saveEvent:event span:EKSpanThisEvent error:&err]) {
if(DEBUG_MODE){NSLog(@"Event Send");}
}else {
if(DEBUG_MODE){ NSLog(@"%@",[err localizedDescription]);}
}
UITableViewCell *cell=[tbl_showing_Details cellForRowAtIndexPath:[NSIndexPath indexPathForRow:clickedTag inSection:0]];
UIButton *btnSync = (UIButton*)[cell viewWithTag:TAG_SHOWINGCALBUTTON];
// [AppStatus showAlert:MESSAGE_TITLE message:MESSAGE_SYNC_SUCCESS delegate:self cancelButtonTitle:MESSAGE_OK];
[btnSync setImage:[UIImage imageNamed:@"calendfer_icon_selected.png"] forState:UIControlStateNormal];
[btnSync setEnabled:FALSE];
}
}
int clickedTag = alertView.tag;
NSString *alarmDate = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"appointmentdate"];
NSString *startTime = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"starttime"];
startTime = [NSString stringWithFormat:@"%@ %@",alarmDate,startTime];
NSString *endTime = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"endtime"];
NSString *title = (NSString*)[[self.table_showing_array objectAtIndex:clickedTag] objectForKey:@"address"];
endTime = [NSString stringWithFormat:@"%@ %@ ",alarmDate,endTime];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
NSDateFormatter *df= [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy HH:mm"];
NSDate *FireDate = [df dateFromString:startTime];
NSDate *EndDate = [df dateFromString:endTime];
if([self checkifAppointmentExists:FireDate withEndDate:EndDate withTitle:title]== TRUE)
{
}
else{
event.startDate = FireDate;
event.endDate = EndDate;
event.allDay = YES;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSArray *arrAlarm = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:FireDate]];
event.alarms= arrAlarm;
NSError *err;
if ([eventStore saveEvent:event span:EKSpanThisEvent error:&err]) {
if(DEBUG_MODE){NSLog(@"Event Send");}
}else {
if(DEBUG_MODE){ NSLog(@"%@",[err localizedDescription]);}
}
UITableViewCell *cell=[tbl_showing_Details cellForRowAtIndexPath:[NSIndexPath indexPathForRow:clickedTag inSection:0]];
UIButton *btnSync = (UIButton*)[cell viewWithTag:TAG_SHOWINGCALBUTTON];
// [AppStatus showAlert:MESSAGE_TITLE message:MESSAGE_SYNC_SUCCESS delegate:self cancelButtonTitle:MESSAGE_OK];
[btnSync setImage:[UIImage imageNamed:@"calendfer_icon_selected.png"] forState:UIControlStateNormal];
[btnSync setEnabled:FALSE];
}
}
No comments:
Post a Comment