Address book change callback registered with ABAddressBookRegisterExternalChangeCallback is never called (iOS 8)

主宰稳场 提交于 2019-12-24 00:29:05

问题


I've found plenty of examples around this but after reading the entire ABAddressBook documentation I'm still not able to figure out why, in my case, my change callback is not being called. I simply set up an address book and register a callback function for it.

I can access the address book just fine, but the callback function is never called no matter how much I change contacts in the Contacts app and then reopen my app. Is there any reason that the callback would never be called? I've already made sure I don't release the address book or unregister the callback.

The init code:

// Set up address book API.
CFErrorRef *error = NULL;
_addressBook = ABAddressBookCreateWithOptions(NULL, error);
if (error) {
    NSLog(@"Could not initialize address book: %@", CFBridgingRelease(CFErrorCopyFailureReason(*error)));
} else {
    ABAddressBookRegisterExternalChangeCallback(_addressBook, RogerAddressBookChangeCallback, (__bridge void *)self);
    NSLog(@"Registered callback");
}

The callback function:

void RogerAddressBookChangeCallback(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
    NSLog(@"Address book change");
    ABAddressBookRevert(addressBook);
    RogerAddressBook *instance = (__bridge RogerAddressBook *)context;
    [instance import];
}

I see the log output Registered callback but never Address book change.


回答1:


Actually the code for ABAddressBook is written in C. So you may find difficulties in using the Original ABAddressBook Framework.

So I suggest for using an third party library (Which is just an makeover of C to Obj-C) to access contacts and the Contacts Change.

Here is the link for an popular library https://github.com/Alterplay/APAddressBook

Using the above framework you could easily observe the changes in Address book.

Observe address book external changes

// start observing
[addressBook startObserveChangesWithCallback:^
{
    NSLog(@"Address book changed!");
}];
// stop observing
[addressBook stopObserveChanges];

This library also has lot of options like sorting, filtering etc.




回答2:


Access to address book requires user authorization. If authorization status is kABAuthorizationStatusNotDetermined, your code fails silently, returning non-nil result and producing no error.

I have next code to create address book:

- (ABAddressBookRef)newAddressBookRef
{
    ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();

    if (authorizationStatus == kABAuthorizationStatusAuthorized)
    {
        ABAddressBookRef addressBookRef = nil;

        CFErrorRef error;
        addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);

        return addressBookRef;
    }

    return nil;
}

And next code to explicitly request address book access (usually performed on application start).

typedef void(^AddressBookHelperAccessRequestCompletionHandler)(BOOL accessGiven);

- (void)requestAccessToAddressBook:(ABAddressBookRef)addressBookRef withCompletionHandler:(AddressBookHelperAccessRequestCompletionHandler)completionHandler
{
    ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();

    switch (authorizationStatus)
    {
        case kABAuthorizationStatusNotDetermined:
        {
            // Request access permissions for even for NULL address book reference.
            // When permissions have not been granted yet, all address book references will be equal to NULL
            ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
            {
                if (granted)
                {
                    [self registerForAddressBookChanges];
                }

                if (completionHandler)
                {
                    completionHandler(granted);
                }
            });
            break;
        }
        case kABAuthorizationStatusDenied:
        case kABAuthorizationStatusRestricted:
            [self showNoContactsAccessAlert];
        default:
        {
            if (completionHandler)
            {
                completionHandler(authorizationStatus == kABAuthorizationStatusAuthorized);
            }
            break;
        }
    }
}


来源:https://stackoverflow.com/questions/28921640/address-book-change-callback-registered-with-abaddressbookregisterexternalchange

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