-1

I am developing an app for medicine reminder. The user inserts the medicine name and time from date picker and I create a reminder for each one.

My question: how I can delete a reminder when the user wants to edit the medicine time for any kind of medicine?

This is my code to save a reminder:

 // Get the current date

    NSDate *pickerDate = [datePicker date];
   // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )      fromDate:pickerDate];

    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:pickerDate];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];

    [dateComps setDay:[dateComponents day]];

    [dateComps setMonth:[dateComponents month]];

    [dateComps setYear:[dateComponents year]];

    [dateComps setHour:[timeComponents hour]];

//  // Notification will fire in one minute

    [dateComps setMinute:[timeComponents minute]];

[dateComps setSecond:[timeComponents second]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];

EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])

{
    // iOS 6 and later

    // asks user to allow application to use his device calendar

    [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error)

     {
        if (granted)
        {
            EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];

             reminder.title = [NSString stringWithFormat: @"وقت الدواء"] ;

             reminder.calendar = [eventStore defaultCalendarForNewReminders];

            // NSDate *date = itemDate;

             EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:itemDate];
             [reminder addAlarm:alarm];
             [EventIDArray addObject:alarm];

             NSError *error = nil;
             [eventStore saveReminder:reminder commit:YES error:&error];

             if(error)

                 NSLog(@"unable to Reminder!: Error= %@", error);
        }

     }];

}

// iOS < 6

else

{ EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];

    reminder.title = [NSString stringWithFormat: @"وقت الدواء"] ;

    reminder.calendar = [eventStore defaultCalendarForNewReminders];

    NSDate *date = itemDate;

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];

    [reminder addAlarm:alarm];
    [EventIDArray addObject:alarm];
    NSError *error = nil;

    [eventStore saveReminder:reminder commit:YES error:&error];

    if(error)

        NSLog(@"unable to Reminder!: Error= %@", error);

I use remove reminder but the reminder still active.

[eventStore removeReminder:[EventIDArray objectAtIndex:row] commit:YES error:&error];

Thank you, I appreciate any help.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Fadia Jaradat
  • 199
  • 2
  • 16

2 Answers2

0

To effectively cancel a reminder (EKReminder), you should set the completed property to YES.

This will automatically set completionDate for that EKReminder to the current date.

Apple API Documentation for EKReminder

As mentioned in the above documentation, you can simply set the property to YES to cancel a reminder.

For you I imagine that would look something like this:

(EKReminder *)[EventIDArray objectAtIndex:row].completed = YES;

Assuming that the object returned from:

[EventIDArray objectAtIndex:row] 

is the EKReminder object instance you wish to cancel.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • Thank u John Woods for response, but what do you mean by set the completed property to YES? which property? – Fadia Jaradat Dec 30 '13 at 10:41
  • Hi Fadia, the EKReminder class itself has a property called 'completed', you need to set this to 'YES' to cancel a given reminder. I have updated the answer. Let me know if you have any more questions on this. – Woodstock Dec 30 '13 at 10:50
  • Hi John, if u notes I add alarm to EventIDArray, what do u see is this correct or I should add itemDate? when I use [EventIDArray objectAtIndex:row].completed = YES; I have this error: Property 'completed' not found on object of type 'id', and when I put all reminders are cancelled EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore]; reminder.completed=YES; – Fadia Jaradat Dec 30 '13 at 11:48
  • Hi Fadia, As mentioned by Mihir, you should cast the id object to an EKReminder. I have updated my answer - apologies for the inaccuracy, I don't have XCode to hand. – Woodstock Dec 30 '13 at 13:16
  • Thank u John and Mihir, after I cast the object the same error Property 'completed' not found on object of type 'id' – Fadia Jaradat Dec 30 '13 at 13:26
  • It sounds as though you are not casting correctly, can you post your code as it currently is and also post a complete copy of the compiler error? – Woodstock Dec 30 '13 at 13:42
  • This is the code for turn off reminder: EKEventStore *eventStore = [[EKEventStore alloc] init]; NSError *error = nil; [eventStore removeReminder:[EventIDArray objectAtIndex:row] commit:YES error:&error]; (EKReminder *)[EventIDArray objectAtIndex:row].completed = YES; and the error : Property 'completed' not found on object of type 'id' – Fadia Jaradat Dec 30 '13 at 14:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44168/discussion-between-john-woods-and-fadia-jaradat) – Woodstock Dec 30 '13 at 15:09
0

You need to cast the object returned from the array like this.

(EKReminder *)[EventIDArray objectAtIndex:row].completed = YES;
Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68