Skip to content

Instantly share code, notes, and snippets.

@avwave
Created September 25, 2013 08:54
Show Gist options
  • Save avwave/6696887 to your computer and use it in GitHub Desktop.
Save avwave/6696887 to your computer and use it in GitHub Desktop.
+(void)getCellContacts:(void (^)(NSArray *contactArray))successBlock {
ABAddressBookRef myAddressBook = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(myAddressBook, ^(bool granted, CFErrorRef error) {
NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);
NSMutableArray *contactList = [[NSMutableArray alloc]initWithCapacity:[allPeople count]];
[allPeople enumerateObjectsUsingBlock:^(id record, NSUInteger idx, BOOL *stop) {
CFTypeRef phoneProperty = ABRecordCopyValue((__bridge ABRecordRef)record, kABPersonPhoneProperty);
NSArray *phones = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
CFRelease(phoneProperty);
NSString* contactName = (__bridge NSString *)ABRecordCopyCompositeName((__bridge ABRecordRef)record);
NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init];
if (!contactName) {
[newRecord setObject:@"(no name)" forKey:@"name"];
}
else {
[newRecord setObject:contactName forKey:@"name"];
}
//[contactName release];
NSMutableString *newPhone = [[NSMutableString alloc] init];
for (NSString *phone in phones) {
//NSString *fieldData = [NSString stringWithFormat:@"%@: %@", contactName, phone];
if(![newPhone isEqualToString:@""])
[newPhone appendString:@", "];
[newPhone appendString:phone];
}
[newRecord setObject:newPhone forKey:@"cellnumber"];
if (![newPhone isEqualToString:@""]) {
[contactList addObject:newRecord];
}
successBlock(contactList);
}];
CFRelease(myAddressBook);
NSLog(@"Final data: %@", contactList);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment