Contents

ABAddressBookRequestAccessCompletionHandler

Definition for a block callback invoked when an access request has completed.

Declaration

typealias ABAddressBookRequestAccessCompletionHandler = (Bool, CFError?) -> Void

Discussion

Address book request access completion handler blocks are used with ABAddressBookCreateWithOptions(_:_:). If you had a view controller that wanted to display the count of users with the name “Smith” in the address book, you might implement something like the code shown in the following code listing.

Listing 1. Sample implementation using ABAddressBookRequestAccessCompletionHandler

@implementation APLViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view
    CFErrorRef myError = NULL;
    ABAddressBookRef myAddressBook = ABAddressBookCreateWithOptions(NULL, &myError);
    APLViewController * __weak weakSelf = self;  // avoid capturing self in the block
    ABAddressBookRequestAccessWithCompletion(myAddressBook,
      ^(bool granted, CFErrorRef error) {
        if (granted) {
            NSArray *theSmiths = CFBridgingRelease(
              ABAddressBookCopyPeopleWithName(myAddressBook,
                CFSTR("Smith")
              )
            );
            weakSelf.numberOfSmiths = [theSmiths count];
        } else {
            // Handle the case of being denied access and/or the error.
        }
        CFRelease(myAddressBook);
    });
}
 
...
 
@end

See Also

Deprecated