EKEventStoreChangedNotification not firing

左心房为你撑大大i 提交于 2019-12-01 23:37:34

问题


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?


回答1:


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



回答2:


You have to ensure that EKEventStore object stays in memory for use.

These scenarios will not work with ARC:

@property (weak, nonatomic) EKEventStore *eventStore;
self.eventStore = [[EKEventStore alloc] init];

.

EKEventStore *eventStore = [[EKEventStore alloc] init];

This scenarios will work with ARC:

@property (strong, nonatomic) EKEventStore *eventStore;

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];
    }
}];



回答3:


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



来源:https://stackoverflow.com/questions/20713360/ekeventstorechangednotification-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!