在地址簿更新时收到通知

时间:2014-07-03 15:46:18

标签: ios tableview addressbook

我正在尝试使用预定义方法

addressBook获取更新
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);

void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
    NSLog(@"AddressBook Changed");
    [self getContactsFromAddressBook];
}

我在ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);中调用application:didFinishLaunchingWithOptions,然后我执行回调方法,如何在c方法中使用self?如果我不能使用我的对象,如何更新我的tableview?

1 个答案:

答案 0 :(得分:5)

您不能在该函数中直接使用self - 但是当您注册更改回调时,您将传递self作为上下文,因此它将作为addressBookChanged函数中的参数传递。

void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
    NSLog(@"AddressBook Changed");
    YourClass *yourInstance = (__bridge YourClass *)(context)
    [yourInstance getContactsFromAddressBook];
}

更具体到您的课程 -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
{
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self.wkListVC);     
    return YES; 
}

void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) 
{     
    NSLog(@"AddressBook Changed");     
    wbkABViewControllerTableViewController *myVC = (__bridge wbkABViewControllerTableViewController *)context;
    [myVC getPersonOutOfAddressBook]; 
}

当您注册更改回调时,请确保self.wkListVC不是nil。