EKEventStoreChangedNotification 未触发通知

4

我目前正在尝试使用EventKit,并尝试在本机日历应用程序中添加/修改/删除日历条目时触发EKEventStoreChangedNotification,但在请求访问日历权限,确认已获授权并注册通知后,该通知似乎无法触发。

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

选择器从未被调用。尝试了块语法,也不起作用。
所以我想我做错了什么,并找到了这个示例代码,据说有工作通知,但即使在拉取该项目并确保addObserver行被调用后,我也无法在修改日历时看到选择器被调用。
有什么想法可以进一步调试吗?

你是在模拟器上尝试还是在设备上? - Léo Natan
@VahurRoosimaa 请问您能否具体说明一下您在哪里提到了上述代码以及您是如何进行测试的? - Easwaramoorthy K
@VahurRoosimaa 另外,你能贴出 storeChanged: 方法的代码吗?它是否有通知对象作为参数? - Easwaramoorthy K
3个回答

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

不要将 object: 设置为 nil。这会导致您在应用程序中收到任何其他 EKEventStore 对象的虚假通知。请将对象设置为 self.eventStore - uliwitness

1

我认为你需要将eventStore作为对象添加。可以参考这个例子,对我很有效。

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

观察日历数据库的外部更改


这并不是必须的。(话虽如此,你最好指定对象,否则你将会收到应用程序中每个 EKEventStore 的通知,如果有多个,这可能会变得很慢...) - tc.

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

1
我遇到了相同的问题,以上的解决方案都对我没有起作用。 - real 19
我也遇到了这个问题。在10.9中有人成功过吗?我已经在10.10 Yosemite中尝试了相同的代码,它可以正常工作,但在10.9中不行。@Ramshad,你找到解决方法了吗? - Z S
@ZS:确保你的 EKEventStore 没有被解除分配。这是唯一的诀窍。 :) - Shamsudheen TK
不要将 object: 设置为 nil。这会导致您在应用程序中收到任何其他 EKEventStore 对象的虚假通知。请将对象设置为 self.eventStore - uliwitness

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接