Skip to content

Instantly share code, notes, and snippets.

@tiagobbraga
Last active December 18, 2015 05:59
Show Gist options
  • Save tiagobbraga/5737178 to your computer and use it in GitHub Desktop.
Save tiagobbraga/5737178 to your computer and use it in GitHub Desktop.
fetch contacts io6
- (void)fetchContacts:(void (^)(NSArray *contacts))success failure:(void (^)(NSError *error))failure {
if (ABAddressBookRequestAccessWithCompletion) {
// on iOS 6
CFErrorRef err;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &err);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
// ABAddressBook doesn't gaurantee execution of this block on main thread, but we want our callbacks to be
dispatch_async(dispatch_get_main_queue(), ^{
if (!granted) {
failure((__bridge NSError *)error);
} else {
readAddressBookContacts(addressBook, success);
}
});
});
} else {
// on iOS < 6
ABAddressBookRef addressBook = ABAddressBookCreate();
readAddressBookContacts(addressBook, success);
}
}
static void readAddressBookContacts(ABAddressBookRef addressBook, void (^completion)(NSArray *contacts)) {
// do stuff with addressBook
NSArray *contacts = @[];
completion(contacts);
}
@jamlawas
Copy link

how can i call the fetchContacts method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment