I have a table view that displays the names of all contacts on the device. These names are coming from an array called contactsArray
. For each contact
object I'm getting the phoneNumbers
object and pulling the numbers into another array called phoneNumberArray
. When I put them into my table view it displays each contact with it's corresponding number...but only for so long. When I get down a few dozen rows the numbers aren't matched up with the correct contact anymore because some contact
objects contained a phoneNumbers
object with multiple phone numbers. How can I get only the first phone number of each object so that I have an equal number of contacts and phone numbers?
Here is my code:
@property (nonatomic, strong) NSMutableArray *contactsArray;
@property (nonatomic, strong) NSMutableArray *phoneNumberArray;
@end
@implementation Contacts
- (void)viewDidLoad
{
[super viewDidLoad];
self.phoneNumberArray = [[NSMutableArray alloc]init];
self.contactsArray = [[NSMutableArray alloc]init];
[self fetchContactsandAuthorization];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.contactsArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"contactCell";
ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
CNContact *contact = self.contactsArray[indexPath.row];
NSString *phone = self.phoneNumberArray[indexPath.row];
NSString *contactName = [NSString stringWithFormat:@"%@ %@",contact.givenName,contact.familyName];
NSString *contactNumber = phone;
cell.name.text = contactName;
cell.number.text = contactNumber;
[cell.inviteBtn addTarget:self action:@selector(openMessagesForContact:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 72;
}
-(void)fetchContactsandAuthorization
{
// Request authorization to Contacts
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
{
CNContactStore *addressBook = [[CNContactStore alloc]init];
NSArray *keysToFetch =@[CNContactFamilyNameKey,
CNContactGivenNameKey,
CNContactPhoneNumbersKey];
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
[addressBook enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
[self.contactsArray addObject:contact];
NSString *phone;
for (CNLabeledValue *label in contact.phoneNumbers) {
phone = [label.value stringValue];
if ([phone length] > 0) {
[self.phoneNumberArray addObject:phone];
}
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self.contactsTableView reloadData];
});
}
}];
}
@end