getting notified when addressbook updates

泪湿孤枕 提交于 2020-01-16 01:52:55

问题


I am trying to get updates from addressBook using the predefined method

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);

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

I am calling ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self); in my application:didFinishLaunchingWithOptions, then i do the callback method, how can use self inside that c method? how can i update my tableview if i can't use my objects?


回答1:


You can't directly use self in that function - but you're passing self as the context when you register for the change callback, so it will be passed as an argument in the addressBookChanged function.

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

to be more specific to your classes -

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

Make sure self.wkListVC is not nil when you register for the change callback.



来源:https://stackoverflow.com/questions/24558169/getting-notified-when-addressbook-updates

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