In my app I need to display all contacts from the device in a custom table viewcontoller when I click contacts button. I found a lot of demos using addressbook framework but in it the contacts are displayed in default device format using ABPeoplePickerNavigationController. But I need to display all contacts in custom view. Is there any way to do that. Also my app should work in iOS7 and above. Can Contacts Framework be used in iOS 7 and 8? I'm using xcode 7.2. Please provide the solutions in objective C as I'm not familier with swift.Thanks in advance.
Asked
Active
Viewed 3,938 times
4
-
http://stackoverflow.com/questions/32669612/how-to-fetch-all-contacts-record-in-ios-9-using-contacts-framework – Birendra Feb 24 '16 at 07:48
-
refer this link for contact frame work – Birendra Feb 24 '16 at 07:48
-
if you need help in addressbook frameworks then i have code for the custom view – Birendra Feb 24 '16 at 07:49
-
thanks for the link. But as I mentioned, my app should run on iOS7 and 8 also. Will contacts framework works on versions below 9? – Betsy Feb 24 '16 at 11:25
-
@Birendra Could you please help me with addressbook to obtain the output. – Betsy Feb 24 '16 at 11:33
-
1https://github.com/Kekiiwaa/ContactsManager see this link and download the code and run the demo project it use the addressbook framework and display the contacts detail in custom table view – Birendra Feb 24 '16 at 11:36
-
Do you want to get the contacts from iPhone? – user3182143 Feb 24 '16 at 11:45
-
hi you have check it? – Birendra Feb 24 '16 at 11:58
-
I solved it for your question. – user3182143 Feb 24 '16 at 12:52
-
@Birendra Thanks dude. That was exactly what I wanted. Will contacts framework works on versions below ios9? – Betsy Feb 24 '16 at 13:16
-
I post the answer please like it so i get the point – Birendra Feb 24 '16 at 13:29
2 Answers
2
In iOS 9 AddressBook Framework is deprecated.Kindly use Contact framework.
I tried, and it works fabulously.
Very Perfect answer is below.
#import "ViewController.h"
#import <Contacts/Contacts.h>
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tableViewContactData;
NSMutableArray *arrayTableData;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayTableData = [[NSMutableArray alloc]init];
[self fetchContactsandAuthorization];
tableViewContactData = [[UITableView alloc] init];
tableViewContactData.frame = CGRectMake(0, 50, 320, 518);
tableViewContactData.delegate = self;
tableViewContactData.dataSource = self;
[tableViewContactData registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.view addSubview:tableViewContactData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//UITAbleView Deleate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayTableData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCell = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
if(cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
}
cell.textLabel.text = arrayTableData[indexPath.row];
return cell;
}
//This is for fetching contacts from iPhone.Also It asks authorization permission.
-(void)fetchContactsandAuthorization
{
// Request authorization to Contacts
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
{
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error)
{
NSLog(@"error fetching contacts %@", error);
}
else
{
NSString *phone;
NSString *fullName;
NSString *firstName;
NSString *lastName;
UIImage *profileImage;
NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
for (CNContact *contact in cnContacts) {
// copy data to my custom Contacts class.
firstName = contact.givenName;
lastName = contact.familyName;
if (lastName == nil) {
fullName=[NSString stringWithFormat:@"%@",firstName];
}else if (firstName == nil){
fullName=[NSString stringWithFormat:@"%@",lastName];
}
else{
fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
}
UIImage *image = [UIImage imageWithData:contact.imageData];
if (image != nil) {
profileImage = image;
}else{
profileImage = [UIImage imageNamed:@"person-icon.png"];
}
for (CNLabeledValue *label in contact.phoneNumbers) {
phone = [label.value stringValue];
if ([phone length] > 0) {
[contactNumbersArray addObject:phone];
}
}
NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
[arrayTableData addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]];
NSLog(@"The contactsArray are - %@",arrayTableData);
}
dispatch_async(dispatch_get_main_queue(), ^{
[tableViewContactData reloadData];
});
}
}
}];
}
@end
The output is
The contactsArray are - (
"John Appleseed",
"Kate Bell",
"Anna Haro",
"Daniel Higgins",
"David Taylor",
"Hank Zakroff"
}

user3182143
- 9,459
- 3
- 32
- 39