Remove reminders using NSPredicate iOS - ios

I am trying to remove reminder using NSPredicates but they are not getting removed.
My code is:
NSPredicate *predicate = [eventStore predicateForIncompleteRemindersWithDueDateStarting:[datepicker1 date] ending:[datepicker2 date] calendars:nil]
[eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders)
{
for (EKReminder *reminder in reminders) {
[eventStore removeReminder:reminder commit:YES error:nil];
} }];

You should also specify the calendars parameters as
EKEventStore *eventStore = [[EKEventStore alloc] init] ;
NSArray *calendarArray = [NSArray arrayWithObject:[eventStore defaultCalendarForNewEvents]];
NSPredicate *predicate = [eventStore predicateForIncompleteRemindersWithDueDateStarting:[datepicker1 date] ending:[datepicker2 date] calendars:calendarArray]
[eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders)
{
for (EKReminder *reminder in reminders) {
[eventStore removeReminder:reminder commit:YES error:nil];
}
}];
I hope this should work.

Related

Add Event to custom calendar crashed the application

I am trying to add an event to custom calendar.
To fetch the calendar list I used below
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if(!granted) {
//error alert
return;
}
NSArray *calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent];
// List of all calendars
// Let user to choose a calendar.
}];
For add an event I used the below code
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
Now when I used the default calendar it's successfully added but when I choose any other calendar the application get crashed and in log it's shows
*** -[EKSource retain]: message sent to deallocated instance 0x7f9bddb1bf50
I am not able to find out the exact problem or for which line this problem arise. Any advice will be appreciable.
You have to check whether that particular calendar allows you to modify it or not
if ([storeObject calendar].allowsContentModifications)
{
//add event now
}
Yes. Your store object is deallocated before completion block starts to run.
The easiest way to fix it - save store object as a field:
EKEventStore *store;
-(void)addEvent:(EventStoreClass *)storeObject
{
store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
[event setCalendar:[storeObject calendar] ? [storeObject calendar] : [store defaultCalendarForNewEvents]];
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
This problem was solved by doing the below.
-(void)addEvent:(EventStoreClass *)storeObject
{
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent *event = [EKEvent eventWithEventStore:store];
[event setTitle:[storeObject eventTitle]];
[event setStartDate:[storeObject eventStartDate]];
[event setEndDate:[storeObject eventEndDate]];
if([storeObject calendar]) {
NSArray *calendars = [store calendarsForEntityType:EKEntityTypeEvent];
__block EKCalendar *calendar;
[calendars enumerateObjectsUsingBlock:^(EKCalendar *cal, NSUInteger idx, BOOL *stop) {
if([[cal title] isEqualToString:[[storeObject calendar] title]]) {
calendar = cal;
*stop = YES;
}
}];
[event setCalendar:calendar];
}
else {
[event setCalendar:[store defaultCalendarForNewEvents]];
}
[store saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
}];
}
The problem was on [storeObject calendar] as this calendar was taken from another store and we are assigning it with different store. So I just took the calendars from the same store and it's work.

How to remove reminder saved as an EKReminder?

I have saved a reminder using EKReminder,now I want to remove this reminder from EKReminder.
I have date and time of the reminder to be removed. I tried using NSPRedicate but it didn't work for me.
I am using this code:
EKEventStore *eventStore = [[EKEventStore alloc] init] ;
EKReminder *reminder = [EKReminder
reminderWithEventStore:eventStore];
reminder.calendar = [eventStore defaultCalendarForNewReminders];
EKAlarm *reminderDate = [EKAlarm alarmWithAbsoluteDate:removeReminder];
[reminder removeAlarm:reminderDate];
NSError *error = nil;
[eventStore removeReminder:reminder commit:YES error:&error];
if (error)
{
NSLog(#"error = %#", error);
}
else{
NSLog(#"Reminder removed successfully");
}
Where removeReminder is my date.
Please suggest me if there is problem with this code.
I found my answer:
EKEventStore *eventStore = [[EKEventStore alloc] init] ;
NSArray *calendarArray = [NSArray arrayWithObject:[eventStore defaultCalendarForNewReminders]];
NSPredicate *predicate = [eventStore predicateForRemindersInCalendars:calendarArray];
[eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders)
{
for (EKReminder *reminder in reminders)
{
//here based on reminder.title i removed the reminder.
[eventStore removeReminder:reminder commit:YES error:nil];
}

iOS calendar repeating event

Good day,
I can't add custom event in standard iOS Calendar that will be repeated (during for example 2 months).
The problem is that my event is just up to the end of current month. It doesn't matter which day it will start - it will be repeating only during this month.
Code:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (!granted)
return;
// setting the values directly for testing purpose.
EKRecurrenceEnd *endRecurrence = nil;
NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];
//Only weekends
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:6]];
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:7]];
endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*7*10]]];
//create the recurrence rule here
EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency: EKRecurrenceFrequencyYearly
interval:1
daysOfTheWeek:daysOfTheWeekArray
daysOfTheMonth:nil
monthsOfTheYear:nil
weeksOfTheYear:nil
daysOfTheYear:nil
setPositions:nil
end:endRecurrence];
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event addRecurrenceRule: recurrence];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
Please change your frequency with EKRecurrenceFrequencyMonthly, currently it's EKRecurrenceFrequencyYearly:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (!granted)
return;
// setting the values directly for testing purpose.
EKRecurrenceEnd *endRecurrence = nil;
NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];
//Only weekends
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:6]];
[daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:7]];
endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*7*10]]];
//create the recurrence rule here
EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency: EKRecurrenceFrequencyMonthly
interval:1
daysOfTheWeek:daysOfTheWeekArray
daysOfTheMonth:nil
monthsOfTheYear:nil
weeksOfTheYear:nil
daysOfTheYear:nil
setPositions:nil
end:endRecurrence];
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event addRecurrenceRule: recurrence];
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];

What format to enter an NSDate in

This is how I am adding an event to Ical I just dont know how to make the start date something other than the current day, as well as set the duration as an all day event.
> EKEventStore *store = [[EKEventStore alloc] init];
> [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
> if (!granted) { return; }
> EKEvent *event = [EKEvent eventWithEventStore:store];
> event.title = #"Hits THermal DC I";
> event.startDate = [NSDate date]; //today
> event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
> [event setCalendar:[store defaultCalendarForNewEvents]];
> NSError *err = nil;
> [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
> NSString *savedEventId = event.eventIdentifier; //this is so you can access this event later
> }];
To create any date with your own values, do this:
NSCalendar* calendar=[NSCalendar currentCalendar];
NSDateComponents* dateComps=[[NSDateComponents alloc] init];
[dateComps setYear:1990];
[dateComps setMonth:11];
[dateComps setDay:10];
NSDate* starDate=[calendar dateFromComponents:dateComps];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Hits THermal DC I";
event.startDate = starDate
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
NSString *savedEventId = event.eventIdentifier; //this is so you can access this event later
}];

create calendar not working in device - ios sdk

I have used following code for creating calendar and adding event in that calendar for my application. Everything works fine in the simulator but the calendar is not created in the device. In privacy it does show that my application has the access of the user calendar.
Can anyone please let me know what am I missing?
Thanks in advance.
// this is added in view did load for access
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// handle access here
}];
//add event to main calendar
EKEventStore *eventStore = [[EKEventStore alloc] init];
// [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// handle access here
// }];
EKSource *localSource = nil;
for (EKSource *source in eventStore.sources)
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
EKCalendar *cal;
NSUserDefaults *calid = [NSUserDefaults standardUserDefaults];
if(![calid valueForKey:#"calid"])
{
//cal = [EKCalendar calendarWithEventStore:eventStore];
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
cal.title = #"my calendar title";
cal.source = localSource;
[eventStore saveCalendar:cal commit:YES error:nil];
NSString *strid= [NSString stringWithFormat:#"%#",cal.calendarIdentifier];
[calid setObject:strid forKey:#"calid"];
NSLog(#"cal id = %#", cal.calendarIdentifier);
}
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = myTextField.text;
event.startDate = SelectedDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:event.startDate];
//[event setCalendar:[eventStore defaultCalendarForNewEvents]];
[event setCalendar:[eventStore calendarWithIdentifier:[calid valueForKey:#"calid"]]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"%#",err);

Resources