I searched the internet for importing iOS
contacts in a UITableViewController
because I have a UITableViewController
as a root for a UINavigationController
and I called it contacts like whatsapp
does and what I have found is how to import the contacts picker with a modal segue and I dont want that I want to import them in a custom cell and edit on them . So how can I do that ?
Asked
Active
Viewed 882 times
-2

Buntylm
- 7,345
- 1
- 31
- 51

user3380361
- 25
- 5
-
but only load array of contact in tableview simple. – Darshan Kunjadiya Mar 27 '14 at 10:12
-
can u tell me how can I make this function echo out fontacts in cells – user3380361 Mar 27 '14 at 10:16
-
@user3380361 you getting contact array from this function and load that array in tableview . but you know how to show data in tableview ? – Darshan Kunjadiya Mar 27 '14 at 10:18
-
possible duplicate of [Get a list of all contacts on iOS](http://stackoverflow.com/questions/3747844/get-a-list-of-all-contacts-on-ios) – Buntylm Mar 27 '14 at 10:29
-
@user3380361 check this link http://iphoneappcode.blogspot.in/2012/04/how-to-create-uitableview-in-ios-sdk.html – Darshan Kunjadiya Mar 27 '14 at 10:50
-
@user3380361 and also check this link demo in last option for download http://www.icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/ – Darshan Kunjadiya Mar 27 '14 at 10:54
1 Answers
2
Try this function this is working for me.
-(NSArray *)getAllContacts
{
CFErrorRef *error = 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) {
#ifdef DEBUG
NSLog(@"Fetching contact info ----> ");
#endif
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++)
{
ContactsData *contacts = [ContactsData new];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
contacts.firstNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
contacts.lastNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (!contacts.firstNames) {
contacts.firstNames = @"";
}
if (!contacts.lastNames) {
contacts.lastNames = @"";
}
// get contacts picture, if pic doesn't exists, show standart one
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
contacts.image = [UIImage imageWithData:imgData];
if (!contacts.image) {
contacts.image = [UIImage imageNamed:@"NOIMG.png"];
}
//get Phone Numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (ABMultiValueGetCount(multiPhones)<0)
{
contacts.phonenumber=@"";
}
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
contacts.phonenumber=phoneNumber;
[phoneNumbers addObject:phoneNumber];
NSLog(@"All numbers %@", phoneNumbers);
}
[contacts setNumbers:phoneNumbers];
//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);
}
[contacts setEmails:contactEmails];
[items addObject:contacts];
[Contactarray addObject:contacts];
#ifdef DEBUG
NSLog(@"Person is: %@", contacts.firstNames);
// NSLog(@"Phones are: %@", contacts.numbers);
// NSLog(@"Email is:%@", contacts.emails);
#endif
}
NSLog(@"%@",items);
return items;
} else {
#ifdef DEBUG
NSLog(@"Cannot fetch Contacts :( ");
#endif
return NO;
}
}
i hope this code useful for you .

Darshan Kunjadiya
- 3,323
- 1
- 29
- 31
-
thanks alot for your reply ... but i have a question can you post for me how the whole TableViewController will be ? – user3380361 Mar 27 '14 at 10:11