问题
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