How to retrive contacts from contacts list in iphone programming?

Leave a Comment
Here i am giving simple demo code for retrieving contacts from contact list in iphone. This is very simple basic demo for retrieving contacts but you can find all details of contacts like first name, last name, phone no , email , address and all things from address book . Here i am giving simple code snippets  with source code.
first of all simple class file for all details.

person.h

#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *fullName;
@property (nonatomic, strong) NSString *homeEmail;
@property (nonatomic, strong) NSString *workEmail;
@end

person.m

#import "Person.h"
@implementation Person
- (id)init
{
    self = [super init];
    if (self) {
       
    }
    return self;
}



@end

For displaying contacts details
contactviewcontroller.h
#import <UIKit/UIKit.h>
#import "Person.h"
@interface ContactViewController : UIViewController
@property (nonatomic, strong) Person *person;
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *homeEmail;
@property (nonatomic, strong) NSString *workEmail;
- (id)initWithPerson:(Person *)person;
@end
contactviewcontroller.m
#import "ContactViewController.h"

@interface ContactViewController ()
@property (nonatomic, strong) IBOutlet UILabel *firstNameLabel;
@property (nonatomic, strong) IBOutlet UILabel *lastNameLabel;
@property (nonatomic, strong) IBOutlet UILabel *homeEmailLabel;
@property (nonatomic, strong) IBOutlet UILabel *workEmailLabel;

@end
@implementation ContactViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initWithPerson:(Person *)person
{
    self = [super initWithNibName:@"ContactViewController" bundle:nil];
    if (self) {
        _firstName = [person.firstName copy];
        _lastName = [person.lastName copy];
        _homeEmail = [person.homeEmail copy];
        _workEmail = [person.workEmail copy];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.title = @"Contact Details";
   
    self.firstNameLabel.text = _firstName;
    self.lastNameLabel.text = _lastName;
    self.homeEmailLabel.text = _homeEmail;
    self.workEmailLabel.text = _workEmail;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
 Display  contacts in tableview

ViewController .h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController .m

#import "ViewController.h"
#import "Person.h"
//#import "AppDelegate.h"
#import "ContactViewController.h"
#import <AddressBook/AddressBook.h>
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *tableData;
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    self.title = @"Contacts";
   
    self.tableData = [[NSMutableArray alloc] init];
   
    [self getPersonOutOfAddressBook];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)getPersonOutOfAddressBook
{
    CFErrorRef error = NULL;
   
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
   
    if (addressBook != nil)
    {
        NSLog(@"Succesful.");
       
        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
       
        NSUInteger i = 0;
        for (i = 0; i < [allContacts count]; i++)
        {
            Person *person = [[Person alloc] init];
           
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
           
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
            NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
           
            person.firstName = firstName;
            person.lastName = lastName;
            person.fullName = fullName;
           
            //email
            ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
           
            NSUInteger j = 0;
            for (j = 0; j < ABMultiValueGetCount(emails); j++)
            {
                NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                if (j == 0)
                {
                    person.homeEmail = email;
                NSLog(@"person.homeEmail = %@ ", person.homeEmail);
                }
               
                else if (j==1)
                    person.workEmail = email;
            }
           
            [self.tableData addObject:person];
        }
    }
   
    CFRelease(addressBook);
}
#pragma mark TableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Identifier";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
   
    Person *person = [self.tableData objectAtIndex:indexPath.row];
   
    cell.textLabel.text = person.fullName;
    cell.detailTextLabel.text=person.lastName;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Person *person = [self.tableData objectAtIndex:indexPath.row];
    ContactViewController *contactViewController = [[ContactViewController alloc] initWithPerson:person];
    [self.navigationController pushViewController:contactViewController animated:YES];
}


@end
Output Screen:



Download Complete Source Code


0 comments:

Post a Comment