Last active
December 18, 2015 05:59
-
-
Save tiagobbraga/5737178 to your computer and use it in GitHub Desktop.
fetch contacts io6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can i call the fetchContacts method?