I use this code below to add event , my code called many times so i have duplicate event , is there any idea to prevent duplicate
Thank You in advance
EKEvent *event = [EKEvent eventWithEventStore:es];
NSDateFormatter *dateFormats = [[NSDateFormatter alloc]init];
[dateFormats setDateFormat:#"yyy-MM-dd"];
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] init];
date1 = nil;
date2 = nil;
date1 = [dateFormats dateFromString:event2.from_date];
date2 = [dateFormats dateFromString:event2.to_date];
event.title = event2.event_title;
event.allDay = NO;
event.startDate = date1;
event.endDate = date2;
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f *60.0f *24]];
//put here if start and end dates are same
[event setCalendar:[es defaultCalendarForNewEvents]];
[es saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
I'm basing my answer off this tutorial Calendar event management tutorial. So of course if things don't line up correctly, please reference it.
EKEvent *event = [EKEvent eventWithEventStore:es];
NSDate *date1 = [NSDate new];
NSDate *date2 = [NSDate new];
date1 = nil;
date2 = nil;
date1 = [dateFormats dateFromString:event2.from_date];
date2 = [dateFormats dateFromString:event2.to_date];
event.title = event2.event_title;
event.allDay = NO;
event.startDate = date1;
event.endDate = date2;
NSPredicate *predicate = [es predicateForEventsWithStartDate:event2.from_date endDate:event2.to_date calendars:nil];
NSArray *eventsOnDate = [es eventsMatchingPredicate:predicate];
__block BOOL eventExists = NO;
[eventsOnDate enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
EKEvent *eventToCheck = (EKEvent*)obj;
if([event.title isEqualToString:eventToCheck.title])
{
eventExists = YES;
*stop = YES;
}
}];
if(! eventExists) {
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f *60.0f *24]];
//put here if start and end dates are same
[event setCalendar:[es defaultCalendarForNewEvents]];
[es saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
NSError *saveEventError;
[es saveEvent:event span:EKSpanThisEvent error: &saveEventError];
if(saveEventError) {
//ERROR OCCURED WHILE SAVING
} else {
//SUCCESSFULLY SAVED
}
} else {
// EVENT ALREADY EXISTS
}
Related
i am creating an iOS application, which get dates from the server and add them events to the calendar:
I'm adding them with this code :
- (void)addReminderWithTitle:(NSString *)title date:(NSString *)date {
NSString *dateString = date;
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFromString dateByAddingTimeInterval:-60*30];
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
event.startDate = dateFromString;
//[NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [eventStore defaultCalendarForNewEvents];
NSError *err = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}}];
}
and I need a function, that will delete all the events on a specific action from the user.
any help?
You need to get an array of all events from calendar and filter them in an array by some identifier, date or title so that you can get your own added events.
// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil];
// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];
Then loop over the array of events and delete them like this
EKEventStore* store = [[EKEventStore alloc] init];
EKEvent* event2 = [store eventWithIdentifier:[arrayofCalIDs objectAtIndex:i]];
if (event2 != nil) {
NSError* error = nil;
[store removeEvent:event2 span:EKSpanThisEvent error:&error];
}
I am new to programming I have made a Reminder App which saves the events on iCal and now i want to delete that events through the coding in the project, my code for creating events in iCal is below-
-(void) setReminderInPhone {
NSString *dateWithTime = [NSString stringWithFormat:#"%# %#",calenderDtl.dateInString,calenderDtl.openTimeUTC];
NSString *mrktName = mrktDtl.marketName;
//dateWithTime = #"2016-04-02 04:10 am";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = #"yyyy-MM-dd hh:mm a";
[fmt setAMSymbol:#"am"];
[fmt setPMSymbol:#"pm"];
fmt.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSDate *utc = [fmt dateFromString:dateWithTime];
fmt.dateFormat = #"yyyy-MM-dd hh:mm a";
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSDate *localDate = [fmt dateFromString:local];
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = mrktName;
event.startDate = localDate; //test
event.endDate = [event.startDate dateByAddingTimeInterval:60*2];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}
Try following to remove event from Calendar.
EKEvent *eventToRemove = [eventStore eventWithIdentifier:eventIdentifier];
if (eventToRemove)
{
NSError* error = nil;
[eventStore removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
}
You will get the Value of eventIdentifier at the time of creating event in iCalendar as following :
//Adding Events to default iOS calendar for Making Reminders
-(NSString *)addEventToCalendar: (NSString *)title withDate:(NSString *)date{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"Pregnancy Workout Advisor - Event: %#",title];
event.startDate = [dateFormatter dateFromString:date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.allDay = YES;
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeInterval:9*60*60 sinceDate:[dateFormatter dateFromString:date]]];
[event addAlarm:alarm];
event.calendar = [eventStore defaultCalendarForNewEvents];
NSError *err = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
return event.eventIdentifier; }
In this above method, you will get a eventIdentifier for each event which you're adding to calendar. Store the eventIdentifier to CoreData/Sqlite, whatever..
So that, when you need to delete that event, just give that eventIdentifier. This will surely delete from Calendar.
Hope it helps...
try this code, its working for me,
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent* eventToRemove = [store eventWithIdentifier:[eventIdendifier objectAtIndex:indexPath.row]];
if (eventToRemove) {
[eventToRemove setCalendar:[store defaultCalendarForNewEvents]];
NSError* err = nil;
[store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&err];
dispatch_async(dispatch_get_main_queue(), ^{
[eventIdendifier removeObjectAtIndex:indexPath.row];
});
}
}];
i have give eventIdentifier value, its NSMutableArray value. first you add the event in Event Store you had each event got each event identifier value, its automatically save to eventIdentifier and get the identifier value method is below,
EKEventStore *store = [EKEventStore new];
if ([store respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
/* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if ( granted )
{
NSPredicate *predicate = [store predicateForEventsWithStartDate:date1 endDate:[date1 dateByAddingTimeInterval:23.96 * 60.0 * 60.0] calendars:nil];
NSMutableArray *eventsVal = (NSMutableArray *)[store eventsMatchingPredicate:predicate];
for (int i = 0; i<eventsVal.count; i++) {
[eventIdendifier addObject:[[eventsVal objectAtIndex:i] eventIdentifier]];
}
}
}];
}
its predicate the start date to end date check then got all events particular date and also add object from eventIdentifier value if the value help to show event Values, remove events etc
hope its helpful
I am using the below two methods to create Events in the device calendar, Events is my managedObject class.
+ (void)createCalendarEvents:(NSArray *)eventsArray
{
EKEventStore *eventStore = [[SharedLoader sharedInstance] eventStore];
NSLog(#"eventStore: %#",eventStore);
BOOL needsToRequestAccessToEventStore = NO; // iOS 5 behavior
if ([[EKEventStore class] respondsToSelector:#selector(authorizationStatusForEntityType:)])
needsToRequestAccessToEventStore = ([EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent] == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
for(Events *eventObject in eventsArray)
[self addEventToCalendar:eventObject forEventStoreObject:eventStore];
NSError *commitError;
if([eventStore commit:&commitError])
{
NSLog(#"Events commited to device calendar");
}
else
{
NSLog(#"Events could not be commited to device calendar: %#",[commitError localizedDescription]);
}
});
}
}];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
for(Events *eventObject in eventsArray)
[self addEventToCalendar:eventObject forEventStoreObject:eventStore];
NSError *commitError;
if([eventStore commit:&commitError])
{
NSLog(#"Events commited to device calendar");
}
else
{
NSLog(#"Events could not be commited to device calendar: %#",[commitError localizedDescription]);
}
});
}
}
+ (BOOL)addEventToCalendar:(Events *)eventObject forEventStoreObject:(EKEventStore *)eventStore
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *date =[NSString stringWithFormat:#"%#", eventObject.eventStartDate];
NSPredicate *predicateForEventsOndate = [eventStore predicateForEventsWithStartDate: eventObject.eventStartDate endDate:[[NSDate alloc] initWithTimeInterval:600 sinceDate:[dateFormatter dateFromString:date]] calendars:nil]; // nil will search through all calendars
NSArray *eventsOnDate = [eventStore eventsMatchingPredicate:predicateForEventsOndate];
BOOL eventExists = NO;
EKEvent *existingEvent = nil;
for (EKEvent *eventToCheck in eventsOnDate) {
if ([eventToCheck.title isEqualToString:eventObject.eventTitle]) {
eventExists = YES;
existingEvent=eventToCheck;
break;
}
}
if (eventExists == NO) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title =[NSString stringWithFormat:#"%#",eventObject.eventTitle];
NSLog(#"calendar event: %#", event.title);
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[tempFormatter setLocale:[NSLocale currentLocale]];
NSString *dateandtime =[NSString stringWithFormat:#"%#",eventObject.eventStartDate];
NSLog(#"dateandtime: %#",dateandtime);
NSString *dateandtimeend =[NSString stringWithFormat:#"%#",eventObject.eventEndDate];
NSLog(#"dateandtime: %#",dateandtimeend);
event.location = [NSString stringWithFormat:#"%#",eventObject.eventVenue];;
event.startDate =[tempFormatter dateFromString:dateandtime];
event.endDate = [tempFormatter dateFromString:dateandtimeend];
event.notes =eventObject.eventPurpose;
/*[event addAlarm:[EKAlarm alarmWithAbsoluteDate:event.startDate]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];*/
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
if([eventStore saveEvent:event span:EKSpanThisEvent error:&err])
{
NSLog(#"New Event Saved Successfully");
return YES;
}
else
{
NSLog(#"New Event could not be saved: %# %#",[err localizedDescription],err);
return NO;
}
}
else
{
existingEvent.title =[NSString stringWithFormat:#"%#",eventObject.eventTitle];
NSLog(#"calendar event: %#", existingEvent.title);
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[tempFormatter setLocale:[NSLocale currentLocale]];
NSString *dateandtime =[NSString stringWithFormat:#"%#",eventObject.eventStartDate];
NSLog(#"dateandtime: %#",dateandtime);
NSString *dateandtimeend =[NSString stringWithFormat:#"%#",eventObject.eventEndDate];
NSLog(#"dateandtime: %#",dateandtimeend);
existingEvent.location = [NSString stringWithFormat:#"%#",eventObject.eventVenue];;
existingEvent.startDate =[tempFormatter dateFromString:dateandtime];
existingEvent.endDate = [tempFormatter dateFromString:dateandtimeend];
existingEvent.notes =eventObject.eventPurpose;
/*[existingEvent addAlarm:[EKAlarm alarmWithAbsoluteDate:existingEvent.startDate]];
[existingEvent addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
[existingEvent addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];*/
[existingEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
if([eventStore saveEvent:existingEvent span:EKSpanThisEvent error:&err])
{
NSLog(#"Old Event Updated & Saved Successfully");
return YES;
}
else
{
NSLog(#"Existing Event could not be saved: %#",[err localizedDescription]);
return NO;
}
}
}
My events are saved successfully and also I see them on the device calendar but the events are in 2001 and the events disappear after some doing some scroll in the list of events. Need help in this.
I am setting a calendar event from code in my app, and setting the date from my date picker. Here is what I have:
-(void)setCalenderEvent
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
NSDate *startDate = [[NSDate alloc] init];
NSDate *endDate = [[NSDate alloc] init];
event.title = #"Test from date picker";
// event.startDate = startDate;
// event.endDate = endDate;
NSDate *selectedDate = self.datePickerForReminder.date;
event.startDate = selectedDate;
event.allDay = NO;
event.notes = #"Created by Quick Reminders!!";
/* iOS 6 requires the user grant your application access to the Event Stores */
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
/* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if ( granted )
{
NSLog(#"User has granted permission!");
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if (err == noErr){
NSLog(#"No Error");
}
[startDate release];
[endDate release];
[eventStore release];
}
else
{
NSLog(#"User has not granted permission!");
}
}];
}
}
Can anyone tell me what I am doing wrong?
Thanks!
I was able to get it! You need a span time (add end time).
event.endDate = [selectedDate dateByAddingTimeInterval:30*60];
+(NSString *)setCalendarEvent:(NSString *)title withStartDate:(NSDate *)startDate withEndDate:(NSDate *)endDate withAlarmBefore:(NSTimeInterval )alarmTime{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
event.startDate = startDate;
event.endDate = endDate;
NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-alarmTime];
[myAlarmsArray addObject:alarm1];
event.alarms = myAlarmsArray;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSString* eventId = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
return eventId;
}
whenever I store the date in the calender it doesn't store the actual date .
I want to store the exact date which is coming from CMS how we can prevent the iOS calender by changing its time zone .