Removing an event from iCal does not work when iCal is not in the background
I am trying to remove an event from iCal as soon as I receive a notification. The event is cleared only if iCal is in the background. If the same notification is sent after iCal is closed, the event is not deleted. I am trying to access iCal using this method in MyCalendar.m
+ (void)requestAccess:(void (^)(BOOL granted, NSError *error))callback {
if (eventStore == nil) {
eventStore = [[EKEventStore alloc] init];
}
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:callback];
}
And trying to remove the event using the following method in Appdelegate.m
[MyCalendar requestAccess:^(BOOL granted, NSError *error) {
if (granted) {
if ([[self.launchOptions objectForKey:@"type"] isEqualToString:@"remainder"] || [[self.launchOptions objectForKey:@"type"] isEqualToString:@"cancelAppointment"]) {
if ([[self.launchOptions objectForKey:@"type"]
isEqualToString:@"cancelAppointment"]) {
if (![MyCalendar removeEventWithEventIdentifier:
[self.launchOptions objectForKey:@"eventId"]]) {
}
}
}
}
}];
Removes an event from iCal using the following method in MyCalendar.m
+ (BOOL)removeEventWithEventIdentifier:(NSString *)identifier {
EKEvent *event2 = [eventStore eventWithIdentifier:identifier];
BOOL result = NO;
if (event2 != nil) {
NSError *error = nil;
result = [eventStore removeEvent:event2 span:EKSpanThisEvent error:&error];
}
return result;
}
Thanks in advance!
+3
source to share
1 answer
The event store object must be initialized before use.
+ (BOOL)removeEventWithEventIdentifier:(NSString *)identifier {
EKEventStore* eventStore = [[EKEventStore alloc] init];
EKEvent *event2 = [eventStore eventWithIdentifier:identifier];
BOOL result = NO;
if (event2 != nil) {
NSError *error = nil;
result = [eventStore removeEvent:event2 span:EKSpanThisEvent error:&error];
}
return result;
}
+2
source to share