1

I'm trying to get my contacts list in iOS to automatically add contacts to the contacts list without the user having to manually do it - similiar to how Whatsapp works. My code below simply shows prewritten values for the tableView and allows a bar button item to display the contacts list but not allow it to write any new entries to the tableView.

- (void)viewDidLoad
{


CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++) {

    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"Name:%@ %@", firstName, lastName);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSLog(@"phone:%@", phoneNumber);
    }

}





self.friendsSearch.delegate = self;
self.friendsTableView.delegate = self;
self.friendsTableView.dataSource = self;

friendsListArray = [[NSMutableArray alloc] initWithObjects:
                    @"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten",  @"Eleven", nil];



}


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if(searchText.length == 0){
    isFiltered = NO;
}
else
{
    isFiltered = YES;
    filterDataArray = [[NSMutableArray alloc]init];

    for(NSString *str in friendsListArray){
        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound){
            [filterDataArray addObject:str];


        }

    }
}


[self.friendsTableView reloadData];

}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.friendsSearch resignFirstResponder];
[self.view endEditing:YES];
}

// Table view datasource and delegate methods..


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered){
    return [filterDataArray count];

}
return [friendsListArray count];

return 1;

}

bool isKeyboardVisble = FALSE;

-(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];
}
if(!isFiltered){
    cell.textLabel.text = [friendsListArray objectAtIndex:indexPath.row];
}
else //it is filtered
{
    cell.textLabel.text = [filterDataArray objectAtIndex:indexPath.row];
}
return cell;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)backgroundClick:(id)sender {

[self.view endEditing:YES];
}

- (IBAction)addContact:(id)sender {
ABPeoplePickerNavigationController *peoplePicker =
[[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplePicker animated:YES];
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[self dismissModalViewControllerAnimated:YES];
return NO;
}

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)picker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}

- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}


@end
Top
  • 61
  • 12

0 Answers0