Created
September 25, 2013 08:54
-
-
Save avwave/6696887 to your computer and use it in GitHub Desktop.
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)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