-(NSArray *)getAllContacts
{ CFErrorRef *エラー = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
NSString *firstNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (!firstNames)
firstNames=@"";
NSString *lastNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (!lastNames)
lastNames=@"";
// get contacts picture, if pic doesn't exists, show standart one
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
UIImage *img = [UIImage imageWithData:imgData];
if (!img) {
img = [UIImage imageNamed:@"noImage.png"];
}
//get Phone Numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
[phoneNumbers addObject:phoneNumber];
//NSLog(@"All numbers %@", phoneNumbers);
}
//get contact address
NSMutableArray *contactAddress = [NSMutableArray new];
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
// Individual Person Address From Contacts
for (CFIndex j = 0; j < ABMultiValueGetCount(address); j++) {
CFDictionaryRef addressRef = ABMultiValueCopyValueAtIndex(address, j);
NSDictionary *addressDict = (__bridge NSDictionary*)addressRef;
NSString *Street = ([[addressDict objectForKey:@"Street"] length] == 0) ?
@"" : [addressDict objectForKey:@"Street"];
NSString *city = ([[addressDict objectForKey:@"City"] length] == 0) ? @"" :
[addressDict objectForKey:@"City"];
NSString *State = ([[addressDict objectForKey:@"State"] length] == 0)
? @"": [addressDict
objectForKey:@"State"];
NSString *zipCode = ([[addressDict objectForKey:@"ZIP"] length] == 0) ?
@"" : [addressDict objectForKey:@"ZIP"];
NSString *country = ([[addressDict objectForKey:@"Country"] length] == 0) ?
@"" : [addressDict objectForKey:@"Country"];
NSString *countryCode = ([[addressDict objectForKey:@"CountryCode"] length]
== 0) ? @"" : [addressDict objectForKey:@"CountryCode"];
NSLog(@"%@ %@ %@ %@ %@ %@", Street, city, State, zipCode, country,
countryCode);
[contactAddress addObject:addressDict];
}
//get Contact email
NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge NSString *)contactEmailRef;
[contactEmails addObject:contactEmail];
// NSLog(@"All emails are:%@", contactEmails);
}
if ([contactEmails count] != 0) {
NSData* pictureData = UIImagePNGRepresentation(img);
[dict setObject:firstNames forKey:@"firstname"];
[dict setObject:lastNames forKey:@"lastname"];
[dict setObject:pictureData forKey:@"image"];
[dict setObject:phoneNumbers forKey:@"number"];
[dict setObject:contactEmails forKey:@"email"];
[dict setObject:contactAddress forKey:@"address"];
[items addObject:dict];
}
}
return items;
} else {
return NO;
}
}