user1184342
user1184342

Reputation:

How to access contacts of native addressbook from my table view?

I have a table view and i wanted to show the phone contacts of native addressbook in my table view. plz help me

Upvotes: 0

Views: 753

Answers (2)

Kenneth
Kenneth

Reputation: 94

I think you should retrieve records from local address book into array with the help of ABAddressBookCopyArrayOfAllPeople function and than use this array as a dataSource to your table view. In this code Firstname fills the tableview.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        addressBook = ABAddressBookCreate();
        allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    ...
    }

    @implementation ContactsViewController

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                    {   
                         NSInteger size = CFArrayGetCount(allPeople);
                         return size;
                    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil){
         cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
     }

    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, indexPath.row);

    NSString *cellValue = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

    cell.textLabel.text = cellValue;

    return cell;
    }

Upvotes: 1

Allamaprabhu
Allamaprabhu

Reputation: 865

Include AddressBook frameWork ,From there u can access local contacts

Upvotes: 1

Related Questions