自动将通讯录中的联系人添加到tableview

时间:2014-02-09 17:58:03

标签: ios objective-c uitableview contacts

我正在尝试在iOS中获取我的联系人列表,以自动将联系人添加到联系人列表,而无需用户手动执行此操作 - 类似于Whatsapp的工作方式。我的下面的代码只显示了tableView的预写值,并允许一个条形按钮项显示联系人列表,但不允许它将任何新条目写入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

0 个答案:

没有答案