Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EKEventStoreChangedNotification not firing

So I'm currently playing around with EventKit and was trying to get the EKEventStoreChangedNotification to fire when I add/modify/delete calendar entries in the native Calendar app, but after asking permission to access the Calendar, confirming that I'm authorized and signing up for the notification with

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:nil];

the selector is never called. Also tried the block syntax, which doesn't work either.

So I figured I'm doing something wrong and found this sample code, which supposedly has working notifications, but even after pulling that project and making sure that the addObserver line is getting called, I haven't been able to see the selector being called when I modify the calendar.

Any ideas how to debug this further?

like image 608
zatatatata Avatar asked Oct 24 '25 02:10

zatatatata


2 Answers

Make sure your EKEventStore isn't being deallocated. For example, assign it to a strong property.

The following app logs a string when an edit is made in the stock Calendar app:

#import <EventKit/EventKit.h>

@interface AppDelegate : UIResponder<UIApplicationDelegate>
@property (strong, nonatomic) EKEventStore *eventStore;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.eventStore = [[EKEventStore alloc] init];

    [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eventStoreChangedNotification:) name:EKEventStoreChangedNotification object:nil];
        }
    }];

    return YES;
}

- (void)eventStoreChangedNotification:(NSNotification *)notification {
    NSLog(@"Event store changed");
}

@end
like image 112
jonahb Avatar answered Oct 25 '25 21:10

jonahb


I think you need to add the eventStore as object. Check this example. Works for me.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:eventStore];

Observing External Changes to the Calendar Database

like image 36
ReplyAll Avatar answered Oct 25 '25 21:10

ReplyAll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!