3

In my app I want to get the names and emails of the phone contacts who has emails then list them in a tableview.

Is it possible to create user objects with the name and email fields which are filled by the address book of the phone then put those object in a tableview?

Any clue will be appreciated.

gideon
  • 19,329
  • 11
  • 72
  • 113
death7eater
  • 1,094
  • 2
  • 14
  • 35
  • check this link, also contains the csv file http://stackoverflow.com/questions/7118772/how-to-get-contacts-detail-of-iphone-and-make-csv-file-of-that-contact – Mike May 28 '13 at 08:55

2 Answers2

8

The approach you need to follow is :

First you need to get the reference of the AddressBook and then get all the references of contacts in an array. And then you need to enumerate the array and check whether their email property is nil, if not nil, add that contact to another array and then use that array as a datasource for the tableView :)

If you want, I can provide you with the code for checking the email entry for contact in addressBook and then filter those contacts.

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

    for (int i=0;i < nPeople;i++) { 
        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

        //For username and surname
        ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFStringRef firstName, lastName;
        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];

        //For Email ids
        ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) > 0) {
            [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];

        }

With the help of you will get to know, whether the contact has an email or not, if it has, then you can add that contact reference to other array :) Hope it helps :)

Rajan Balana
  • 3,775
  • 25
  • 42
0

You can follow this link - http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html

ABAddressBook class lets you to access the address book data. And you can create your own custom object to these.

kABPersonEmailProperty lets you get the email property of an ABPerson

nswamy
  • 1,041
  • 9
  • 16