Today we will learn how to get list of all contact from iOS device using Contacts framework. Earlier We used to use AddressBook framework, but this is now deprecated in iOS 9. So we will use the latest Contacts framework.

 

Steps:

  1. Create a new Xcode Project Lets say ContactFrameWorkDemo.
  2.  Now add Contacts library into Your Project.

Screen Shot 2016-08-11 at 11.48.16 PM

  1. Now go to storyBoard and take a tableview in which we will display all the contact details.

Screen Shot 2016-08-12 at 12.13.03 AM

  1. Make Proper IBOutlet connection and set the datasource and delegate method for the tableView.

  2. Now go to Your ViewController.m and write the following code.

#import “ViewController.h”

#import <ContactsUI/ContactsUI.h>

@interface ViewController ()

@property (strong,nonatomic) NSArray *contactDetailArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    _contactDetailArray  = [[NSArray alloc]init];

    

    CNContactStore *cnContactStore = [[CNContactStore alloc] init];

    

    [cnContactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        

        if (granted == YES) {

            

            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];

            

            NSString *containerId = cnContactStore.defaultContainerIdentifier;

            

            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];

            

            NSError *error;

            

          _contactDetailArray = [cnContactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

            

            if (error) {

                

                NSLog(@”error fetching contacts %@”, error);

                

            } else {

                [_tableView reloadData];

                

            }

            

        }

        

    }];

    // Do any additional setup after loading the view, typically from a nib.

}

#pragma mark – tableView delegates  Methods

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return [self.contactDetailArray count];

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    static NSString *simpleTableIdentifier = @”SimpleTableItem”;

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }

    CNContact *CnContact = [_contactDetailArray objectAtIndex:indexPath.row];

    for (CNLabeledValue *label in CnContact.phoneNumbers) {

        NSString *phone = [label.value stringValue];

        if ([phone length] > 0) {

            cell.textLabel.text = [NSString stringWithFormat:@”Name – %@ and  Phone  -%@”,CnContact.givenName,phone];

            

        }

        else{

            cell.textLabel.text = [NSString stringWithFormat:@”Name  -%@”,CnContact.givenName];

        }

    }

     return cell;

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

6. Now run the code and see the desired output. You can download whole Xcode Project from here.

Do share Your Reviews.

Enjoy Coding 🙂

Advertisements