EKEventStoreChangedNotification未触发

时间:2013-12-20 23:08:41

标签: ios iphone objective-c eventkit

所以我正在玩EventKit并且在我在原生日历应用程序中添加/修改/删除日历条目时尝试触发EKEventStoreChangedNotification,但是在获得访问日历的权限后,确认我是通过

授权并注册通知
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:nil];

永远不会调用选择器。还尝试了块语法,它也不起作用。

所以我认为我做错了,发现这个sample code,据说有工作通知,但即使在拉动该项目并确保调用addObserver行后,我也无法做到查看修改日历时调用的选择器。

任何想法如何进一步调试?

3 个答案:

答案 0 :(得分:4)

确保您的EKEventStore未被解除分配。例如,将其分配给强大的属性。

在库存日历应用中进行编辑时,以下应用会记录字符串:

#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

答案 1 :(得分:1)

您必须确保EKEventStore对象保留在内存中以供使用。

使用ARC

时,这些方案无法正常工作
@property (weak, nonatomic) EKEventStore *eventStore;
self.eventStore = [[EKEventStore alloc] init];

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

此方案将工作与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];
    }
}];

答案 2 :(得分:0)

我认为您需要将eventStore添加为对象。检查此示例。适合我。

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

Observing External Changes to the Calendar Database