My UITableView isn't loading any data - ios

I am trying to load Contacts that contain addresses to my TableView. I attempted to debug it and the NSLogs are showing that the data is there. However when I try to load it to my UITableView, it hangs on a black screen and nothing happens. The debugger shows the data is printed to console.
I am using an iOS 7 simulator for testing my project.
#implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_contactsArray = [[NSMutableArray alloc] init];
[self getArrayOfPeople];
NSLog(#"array: %#", self.contactsArray);
}
return self;
}
- (void)getArrayOfPeople {
//ABAddressBookRef addressBook = ABAddressBookCreate();
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
for( CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++ ) {
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, emailIndex );
//ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty);
ABMutableMultiValueRef addressRef= ABRecordCopyValue(person, kABPersonAddressProperty);
int addressCount = (int)ABMultiValueGetCount(addressRef);
if(!addressCount) {
CFErrorRef error = nil;
ABAddressBookRemoveRecord(addressBook, person, &error);
if (error) NSLog(#"Error: %#", error);
} else {
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
NSString *contactAddress = (__bridge NSString *)ABMultiValueCopyValueAtIndex(address, 0);
NSString *name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
if (name) {
NSMutableDictionary *contactDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, #"name",
contactAddress, #"address",
nil];
[self.contactsArray addObject:contactDict];
NSLog(#"%#", self.contactsArray);
}
}
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_contactsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [_contactsArray objectAtIndex:indexPath.row];
return cell;
}
#end
I hope my pasted code above will help find the "bug" thats causing my code to malfunction.

try calling reloadData on your table view after you populate your array
[myTableView reloadData];

You are adding NSDictionary items to the array, but assigning them to cell.textLabel.text in your cellForRowAtIndexPath:. Try
cell.textLabel.text = [[_contactsArray objectAtIndex:indexPath.row] valueForKey:#"name"];

You do this:
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
before requesting access to the address book, so you get -1 and you're stuck in a very consuming loop in getArrayOfPeople. Move the following lines:
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
into the if (accessGranted) block.

Did you modify its launch in the appDidFinishLaunching or something else that you think may have had something to do with its normal launch
Check and try following things to debug the problem.
If its your 1st view ,just check if you have connect view to root of Navigation controller
Try to remove the class from the view and check if view is loading.
Try to add a new view Controller without linking class and add it to your navigation controller and check if its displaying white or black screen.

Related

I want to fetch contact list from phone on tableview in ios?

I have written this code
contactlistvc.h
#import <UIKit/UIKit.h>
#interface ContactListVc : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *contactTable;
NSMutableArray *tableData;
}
#property (strong, nonatomic) IBOutlet UITableView *contactTable;
#property (nonatomic, strong) NSMutableArray *tableData;
contactlistvc.m
#import "ContactListVc.h"
#import <AddressBook/AddressBook.h>
#import "Person.h"
#interface ContactListVc ()
#end
#implementation ContactListVc
#synthesize tableData,contactTable;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
tableData = [[NSMutableArray alloc]init];
contactTable = [[UITableView alloc]init];
contactTable.dataSource = self;
contactTable.delegate = self;
[self getPersonOutOfAddressBook];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [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;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
NSLog(#"Succesful.");
NSLog(#"tabledata %#",tableData);
//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
//4
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
//5
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
//6
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;
}
//7
[self.tableData addObject:person];
}
//8
CFRelease(addressBook);
} else {
//9
NSLog(#"Error reading Address Book");
}
}
But i am not getting the contact list. only successfull is coming on console window. I am having 3 tab bar on one tab of contact i want to show the contact list of phone in tableview. Plz help me , thanks.
You need to first get permission to access the native DB...
- (void)requestPermissionForContactsAccessAndFetch
{
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status != kABAuthorizationStatusAuthorized && status != kABAuthorizationStatusNotDetermined) {
// tell user to enable contacts in privacy settings
NSLog(#"You previously denied access: You must enable access to contacts in settings");
return;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(#"ABAddressBookCreateWithOptions error: %#", CFBridgingRelease(error));
return;
}
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(#"ABAddressBookRequestAccessWithCompletion error: %#", CFBridgingRelease(error));
}
if (granted) {
[self getContactsFromAddressBook:addressBook];
} else {
// tell user to enable contacts in privacy settings
NSLog(#"You just denied access: You must enable access to contacts in settings");
}
CFRelease(addressBook);
});
}
Then you can get all contacts in an array...
- (NSMutableArray*)getContactsFromAddressBook:(ABAddressBookRef)addressBook
{
NSArray *allData = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSInteger contactCount = [allData count];
for (int i = 0; i < contactCount; i++) {
ABRecordRef person = CFArrayGetValueAtIndex((__bridge CFArrayRef)allData, i);
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
if (firstName) {
dictionary[#"firstName"] = firstName;
}
if (lastName) {
dictionary[#"lastName"] = lastName;
}
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount(phones);
if (phoneNumberCount > 0) {
NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, 0));
dictionary[#"phone"] = phone;
}
// or if you wanted to iterate through all of them, you could do something like this...
// for (int j = 0; j < phoneNumberCount; j++) {
// NSString *phone = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, j));
// }
if (phones) {
CFRelease(phones);
}
[arrOfContacts addObject:dictionary];
}
}

Sort UITableView content (Address Book) alphabetically

Below is what I am using to retrieve the contacts list from the device. I want it to be displayed alphabetically but using other examples seen on stack overflow I have been unable to get it to work.
The code below is from a tutorial, what do I need to do to it to sort according to alphabetical order?
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil) {
NSLog(#"Succesful.");
//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
//4
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
//5
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
//6
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;
}
//7
[self.tableData addObject:person];
}
//8
CFRelease(addressBook);
} else {
//9
NSLog(#"Error reading Address Book");
}
}
This is my UITableView code:
- (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;
return cell;
}
I have tried below
[self.tableData sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I have also tried NSSortDescriptor but I don't have a Key to sort by.
You'll need to sort the array of Person objects. Once you have finished adding them all to the array you can sort on the fullName using the following code:
[self.tableData sortUsingComparator:^NSComparisonResult(Person *p1, Person *p2) {
return [p1.fullName compare:p2.fullName];
}];
Alternative
You may want to implement a compare: method on the Person object and perform the comparison there, this will keep sorting logic nicely encapsulated and ensure that anything else that uses Person objects can easily perform sorts without duplicating the code shown above.
#implementation Person
// Mostly likely this implementation will contain more code, not shown for brevity
- (NSComparisonResult)compareByFullName:(Person *)otherPerson {
return [self.fullName compare:otherPerson.fullName];
}
#end
Then you can sort the array with:
[self.tableData sortUsingSelector:#selector(compareByFullName:)];
You need to implement and provide a method to sort a Person record as a selector for the sortUsingSelector method invocation.
I managed to solve it like this.
//keys with fetching properties NSArray *keys = #[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactEmailAddressesKey]; CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
//Order contacts by Surname. request.sortOrder = CNContactSortOrderFamilyName;
--OR YOU CAN--
//Order contacts by Name. request.sortOrder = CNContactSortOrderGivenName;

How to display contacts in tableview - Objective C

I want to create program that will import contacts from adressbook and show them in tableview. I already did code to download contacts from adressbook but when I'm adding them into Array and then trying to show in TableView they don't appear when I'm starting app. Here's code:
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[self getPersonOutOfAddressBook];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (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;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)getPersonOutOfAddressBook
{
//1
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(semaphore);
});
}
if (addressBook != nil) {
NSLog(#"Succesful.");
//2
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//3
NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
//4
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;
[self.tableData addObject:person];
}
//8
CFRelease(addressBook);
} else {
//9
NSLog(#"Error reading Address Book");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
When I'm using debugger it shows that variables firstname, lastname and then fullname have access to adressbook because I can see name or last name of person. But I think there is problem with adding to array because I can't see anything in this array. Could someone help me? I'm beginner with Objective - C so please forbearance :)
To use an array you need to both declare it and create it. You do this by allocating and initializing the array.
self.tableData = [[NSMutableArray alloc] init];

UITableView items not showing

I have a UITableView which reads items from an NSMutableArray called friendsList.
I initialise this array here:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.title = TA(#"Find Friends", #"");
self.navigationItem.rightBarButtonItem = [self createRightNavBarButton];
self.navigationItem.leftBarButtonItem = [self createLeftNavBarButton];
friendsList = [[NSMutableArray alloc]init];
}
return self;
}
I have set it's datasource method for numberOfRows to return friendslist.count if the count is greater than zero.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (friendsList.count) {
return friendsList.count;
}
else{
return 0;
}
}
After I fill the array in another method I'm calling [tableView reloadData] so that the datasource method is called again to read the count of the objects in the array.
- (void)loadContactsFromSource:(ListSource)source
{
if (friendsList) {
[friendsList removeAllObjects];
}
switch (source) {
case LS_Facebook:
break;
case LS_Twitter:
break;
case LS_Contacts:{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, nil);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
kCFAllocatorDefault,
CFArrayGetCount(allPeople),
allPeople
);
CFArraySortValues(
peopleMutable,
CFRangeMake(0, CFArrayGetCount(peopleMutable)),
(CFComparatorFunction) ABPersonComparePeopleByName,
(void*) ABPersonGetSortOrdering()
);
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(peopleMutable, i);
[self copyContactToArray:ref];
}
[tableFriends reloadData];
}
else{
[PopupHandler popupDialogWithTitle:T(#"Error", #"")
message:T(#"Access denied", #"")
delegate:nil];
}
});
break;
}
default:
break;
}
}
Here is the cellForRow method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *currentPerson = [friendsList objectAtIndex:indexPath.row];
cell = [self fillCell:cell withUserInfo:currentPerson];
return cell;
}
The problem is that after this table remains blank, the items from the array are not show. If I touch the table with my finger the items show up instantly, as if the touch somehow refreshes the table. Any ideas what might be causing this?
I'm testing this on an iPhone 5 with iOS 7, built from XCode 5.0.1 .
It looks like ABAddressBookRequestAccessWithCompletion is being called on a separate thread and the block that it runs when completed is not in the main thread. So updating the UI inside the block will not work properly. Try either creating a function and calling that function on the main thread from inside your addressbook return or doing a dispatch_async inside the addressbook return block.
Inside your return block from ABAddressBookRequestAccessWithCompletion dispatch into the main thread by either using dispatch_async or performSelectorOnMainThread.

Showing contact name and no. from address book, throwing exception NSInvalidArgumentException

I'm retrieving Contact name and phone no. from the Address book in my application. I'm printing them in log and it is working fine. But when I try to show them on the table view, I'm getting the exception NSInvalidArgumentException. I have a button on the view controller, pressing which the table view should get populated with the contact names and their no.s:
- (IBAction)syncContacts:(id)sender
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
NSString *contact = (NSString *)CFBridgingRelease(ABRecordCopyCompositeName(ref));
// NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref,kABPersonPhoneProperty);
// if (ABMultiValueGetCount(phoneNumbers) > 0) {
NSString *phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
// }
NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)contact,#"Name",phone,#"phone",nil];
[self.phoneContacts addObject:curContact];
}
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
NSLog(#"%#",self.phoneContacts);
NSLog(#"%i",[self.phoneContacts count]);
}
And the table view methods are:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.phoneContacts count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// cell.textLabel.text = [self.phoneContacts objectAtIndex:indexPath.row];
cell = [self.phoneContacts objectAtIndex:indexPath.row];
return cell;
}
What's wrong with the table view? When the phoneContacts had only the name, it was working fine.([phoneContacts addObject:contact]). But now when I'm adding the dictionary object, it is throwing this exception.
I've made a change.
cell = [[self.phoneContacts objectAtIndex:indexPath.row] objectForKey:#"AddressBook"];
The exception doesn't come now. But nothing is getting shown on screen.
Here's the edited method:
- (IBAction)syncContacts:(id)sender
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < ABAddressBookGetPersonCount(addressBook); i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
// NSNumber *contact = (NSNumber *)ABRecordCopyComposite();// (ref));
NSString *contact = (NSString *)CFBridgingRelease(ABRecordCopyCompositeName(ref));
// NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref,kABPersonPhoneProperty);
// if (ABMultiValueGetCount(phoneNumbers) > 0) {
NSString *phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
// }
// NSDictionary *curContact=[NSDictionary dictionaryWithObjectsAndKeys:(NSString *)contact,#"Name",phone,#"phone",nil];
contact = [contact stringByAppendingString:#" "];
contact = [contact stringByAppendingString:phone];
[self.phoneContacts addObject:contact];
}
tableView.delegate = self;
tableView.dataSource = self;
[tableView reloadData];
}
The table view remains unchanged as given in the original post. It is now working.
The commented line cell.textLabel.text = [self.phoneContacts objectAtIndex:indexPath.row]; doesn't work? Can you set breakpoint and check if the cell's textlabel have the text assigned?
Assigning the cell using
cell = [[self.phoneContacts objectAtIndex:indexPath.row] objectForKey:#"AddressBook"]
wouldn't work, unless you defined phoneContacts to be subclass of UITableViewCell.

Resources