0

I'm having trouble accessing users address books on iOS 6 on certain devices. Oddly it works when I test on my own device.

The app never prompts the user to access the address book and my app is not listed in Settings -> Privacy -> Contacts on their devices (it is on mine).

Here's my code, please let me know if I am doing something wrong?

- (void)addressBookWithCompletion:(void (^)(NSArray *addressBook, NSError *error))completion {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_async(queue, ^{
        CFErrorRef error = NULL;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (error || !granted) {
                NSLog(@"%@", error);
                dispatch_async(dispatch_get_main_queue(), ^{
                    completion(nil, (__bridge NSError *)(error));
                });
            } else {
                CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
                // ... do stuff ... 
                CFRelease(addressBook);
                dispatch_async(dispatch_get_main_queue(), ^{
                    completion(arrayOfContacts, nil);
                });
            }
        });
    });
}
runmad
  • 14,846
  • 9
  • 99
  • 140

1 Answers1

1

I would avoid dispatching everything onto the background queue. If something inside // ... do stuff ... is slow, I would look into creating NSObject-Sublasses that encompass the information from contacts that you need to work on and then dispatch those to do the more complex stuff on a background queue (e.g. sending to server writing to disk)...

Felix Lamouroux
  • 7,414
  • 29
  • 46
  • Thanks, I solved the issue by removing the `dispatch_queue`s. Unfortunately it creates a problem where looping through all contacts in the address book blocks the main thread, but I'll have to look into that further at some point :) – runmad Nov 08 '12 at 17:59