เพื่อรับ Addressbook ใน ios
- (void)retreiveAllContacts
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
if (!people) {
return ;
}
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
CFArrayGetCount(people),
people);
CFArraySortValues(peopleMutable,
CFRangeMake(0, CFArrayGetCount(peopleMutable)),
(CFComparatorFunction) ABPersonComparePeopleByName,
(void*) ABPersonGetSortOrdering());
NSMutableArray *contacts = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(peopleMutable)];
for (CFIndex i = 0; i < CFArrayGetCount(peopleMutable); i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(peopleMutable, i);
int32_t recId = ABRecordGetRecordID(person);
NSString * abId = [NSString stringWithFormat:@"%d", recId];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef emailIds = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString* firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* lastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString* companyName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
NSString* displayName = [firstName ? firstName : @"" stringByAppendingFormat:@" %@", lastName ? lastName : @""];
displayName = [displayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
NSMutableDictionary* contactInfo = [[NSMutableDictionary alloc] init];
if(ABPersonHasImageData(person))
{
CFDataRef imageDataRef = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
NSData * imageData = (__bridge NSData *)imageDataRef;
UIImage * thumbImage = [UIImage imageWithData:imageData];
[contactInfo setObject:thumbImage forKey:@"picture"];
}
if(!firstName)
firstName = @"";
if (!lastName)
lastName = @"";
if(!displayName)
displayName = @"";
if(!companyName)
companyName = @"";
// [contactInfo setObject:[firstName capitalizedString] forKey:kFirstNameKey];
//[contactInfo setObject:[lastName capitalizedString] forKey:kLastNameKey];
[contactInfo setObject:[displayName capitalizedString] forKey:@"name"];
[contactInfo setObject:abId forKey:@"ABID"];
// [contactInfo setObject:companyName forKey:kCompanyNameKey];
NSMutableArray* phoneNumbersList = [[NSMutableArray alloc] init];
for (CFIndex j=0; j < ABMultiValueGetCount(phoneNumbers); j++)
{
NSString* phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, j);
CFStringRef localizedLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers,j);
// NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(localizedLabel);
if( phone)
{
// NSLog(@"validatedPhone: %@", validatedPhone);
[phoneNumbersList addObject:phone];
}
if (localizedLabel) {
// NSLog(@"localizedLabel: %@", localizedLabel);
CFRelease(localizedLabel);
}
}
if(phoneNumbers)
{
// NSLog(@"phoneNumbers: %@", phoneNumbers);
CFRelease(phoneNumbers);
// NSLog(@"phoneNumbers Release: %@", phoneNumbers);
}
[contactInfo setObject:phoneNumbersList forKey:@"phoneNumbers"];
NSMutableArray * emailList = [[NSMutableArray alloc] init];
for (CFIndex j=0; j < ABMultiValueGetCount(emailIds); j++)
{
NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emailIds, j);
CFStringRef localizedLabel = ABMultiValueCopyLabelAtIndex(emailIds, j);
if(email)
{
[emailList addObject:email];
}
}
if(emailIds)
{
CFRelease(emailIds);
}
if(emailList && [emailList count])
[contactInfo setObject:emailList forKey:@"emails"];
if ([phoneNumbersList count] > 0 || [emailList count] > 0) {
[contacts addObject:contactInfo];
}
}
//CFRelease();
CFRelease(people);
if([contacts count])
{
[self createiOSContactsDataSourceWithFeed:contacts];
}
}