Используйте ниже код для извлечения контакта с именем и детали
#import <AddressBook/ABAddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
-(NSMutableArray*)fetchContactList{
ABAddressBookRef addressBook = ABAddressBookCreate();
__block BOOL accessGranted = NO;
if (&ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
else { // We are on iOS 5 or Older
accessGranted = YES;
[self getContactsWithAddressBook:addressBook];
}
if (accessGranted) {
return [self getContactsWithAddressBook:addressBook];
}
return [[NSMutableArray alloc] init];
}
// Get the contacts.
- (NSMutableArray*)getContactsWithAddressBook:(ABAddressBookRef)addressBook {
NSMutableArray *contactList = [[NSMutableArray alloc] init];
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *arrWithoutPhoneContact = [[NSMutableArray alloc] init];
for (int i=0;i < nPeople;i++) {
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
//For username and surname
ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
if (firstName == nil) {
firstName = CFStringCreateWithCString (NULL, "", kCFStringEncodingUTF8);;
}
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
if (lastName == nil) {
lastName = CFStringCreateWithCString (NULL, "", kCFStringEncodingUTF8);;
}
[dOfPerson setValue:[NSString stringWithFormat:@"%@", firstName] forKey:@"name"];
//For Email ids
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0) {
[dOfPerson setValue:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
}
NSData *imgData = (__bridge NSData *)(ABPersonCopyImageData(ref));
UIImage *imgProfile = [[UIImage alloc] initWithData:imgData];
if (imgProfile != nil) {
[dOfPerson setValue:imgProfile forKey:@"image"];
}else{
[dOfPerson setValue:[UIImage imageNamed:@"DefaultProfile"] forKey:@"image"];
}
//For Phone number
NSString* mobileLabel;
if (ABMultiValueGetCount(phones)) {
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) {
mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[dOfPerson setValue:[Utility cleanedPhoneNumber:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j)] forKey:@"Phone"];
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[dOfPerson setValue:[Utility cleanedPhoneNumber:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j)] forKey:@"Phone"];
break ;
}else{
[dOfPerson setValue:[Utility cleanedPhoneNumber:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j)] forKey:@"Phone"];
}
}
}else{
NSLog(@"else");
[arrWithoutPhoneContact addObject:[NSString stringWithFormat:@"%d",i]];
}
//Contact Id
ABRecordID recordID = ABRecordGetRecordID(ref);
[dOfPerson setValue:[NSString stringWithFormat:@"%d",recordID] forKey:@"contactId"];
[contactList addObject:dOfPerson];
}
NSLog(@"Withot Phone %@", arrWithoutPhoneContact);
for (int j = 0; j < arrWithoutPhoneContact.count; j++) {
[contactList removeObjectAtIndex:[[arrWithoutPhoneContact objectAtIndex:j] intValue] - j];
}
NSLog(@"Contacts = %@",contactList);
return contactList;
}
Что вы пробовали до сих пор? – user3182143
Я пробовал то, что написал в коде snipplet. – Eir
@ Ей попробуйте мой код для получения списка контактов http://stackoverflow.com/questions/41719037/list-contacts-with-phone-numbers/41720394#41720394 –