为什么CNContactStoreDidChangeNotification没有回调?

时间:2018-08-29 10:12:00

标签: objective-c

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressBookDidChange:) name:CNContactStoreDidChangeNotification object:nil];

-(void)addressBookDidChange:(NSNotification*)notification{
    NSLog(@"change!!!");
}

我在 NSNotificationCenter 中注册了 CNContactStoreDidChangeNotification ,但是当应用变成bg时,联系人有一些变化(例如,添加一个新联系人),然后应用程序变为活动状态,register方法没有任何回调(没有日志“更改”)。

PS

但是当我添加方法

ABAddressBookRegisterExternalChangeCallback(ABAddressBookCreate(), addressBookChandedCallback, nil);


static void addressBookChandedCallback(ABAddressBookRef ab, 
CFDictionaryRef info, void *context){`

NSLog(@"address change!!!");

}

日志:

地址更改!!!

改变!!!

1 个答案:

答案 0 :(得分:0)

我相信问题出在您的代码中,而不是框架中。如果将其放在视图控制器中,则代码应如下所示:

-(void)viewWillAppear:(BOOL)animated {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressBookDidChange:) name:CNContactStoreDidChangeNotification object:nil];
}

-(void)addressBookDidChange:(NSNotification*)notification
{
    //Handle event here...
    NSLog(@"change!!!");

}