在数组中存储iOS的地址簿并在UITableView中显示

时间:2015-03-29 05:15:07

标签: ios objective-c

我想在数组中存储iOS的地址簿并在UITableView中显示。这是我正在使用的代码。我只想要显示联系人姓名和电话号码。

pragma mark-地址簿方法

 (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    ABPeoplePickerNavigationController *picker= [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;

    [self presentModalViewController:picker animated:YES];


    return NO;
}

 (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person{
    ABMultiValueRef phone = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMultiValueRef personID = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonFirstNameProperty);

    CFStringRef phoneID = ABMultiValueCopyValueAtIndex(phone, 0);
     //CFStringRef personName= ABMultiValueCopyValueAtIndex(personID, 0);
    NSLog(@"%@",[NSString stringWithFormat:@"%@", phoneID]);
    NSLog(@"%@",[NSString stringWithFormat:@"%@", personID]);

}

2 个答案:

答案 0 :(得分:0)

请参阅Rays addressbook tutorial。我认为它应该回答你所有的问题。

答案 1 :(得分:0)

为什么你在文本开头调用选择器导航器?您应该通过添加为右栏按钮项的按钮来调用它。然后选择您想要的人并将其存储在可变数组中。之后关闭控制器并加载表视图。

<强>编辑: 请找到以下代码供您参考。我希望它可以帮助理解它是如何工作的。以下是示例代码的参考link

<强>接口

#import <UIKit/UIKit.h>

@interface JSViewController : UITableViewController

@end

<强>实施

#import "JSViewController.h"
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>

@interface JSViewController ()<ABPeoplePickerNavigationControllerDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *peopleSelected;

@end

@implementation JSViewController

-(NSMutableArray *)peopleSelected
{
    if (!_peopleSelected) {
        _peopleSelected = [NSMutableArray new];
}

return _peopleSelected;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    UIBarButtonItem *selectPerson = [[UIBarButtonItem alloc] initWithTitle:@"Select" style:UIBarButtonItemStyleBordered target:self action:@selector(btnSelectHandler:)];
    self.navigationItem.rightBarButtonItem = selectPerson;


}

- (void)btnSelectHandler:(id)sender
{
    ABPeoplePickerNavigationController *peoplePicker = [ABPeoplePickerNavigationController new];
    peoplePicker.peoplePickerDelegate = self;
    [self.navigationController presentViewController:peoplePicker animated:YES completion:^{

    }];
}

#pragma mark - ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.peopleSelected) {
            [self.tableView reloadData];
        }
    }];
}

iOS 8.0中不推荐使用以下方法。您可以使用该功能

- peoplePickerNavigationController:didSelectPerson:

而不是:

//Deprecated in iOS 8.0.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSString *name =  (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    ABMultiValueRef phoneNumbers = (ABRecordCopyValue(person, kABPersonPhoneProperty));
    NSString *mobile = @"";
    NSString *label = @"";
    for (CFIndex i=0 ; i<ABMultiValueGetCount(phoneNumbers); i++) {
        label = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
        if ([label isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
            mobile = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        }else if([label isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]){
            mobile = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
            break;
        }
    }

    NSDictionary *dict = @{@"name":name,
                           @"phone":mobile};

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.phone == %@ && self.name == %@",mobile,name];

    NSArray *personAlready = [self.peopleSelected filteredArrayUsingPredicate:predicate];
    NSLog(@"personAlready %@",personAlready);
    if (!personAlready.count) {
        [self.peopleSelected addObject:dict];
    }

    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{

    return YES;
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.peopleSelected.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSDictionary *dict = self.peopleSelected[indexPath.row];
    cell.textLabel.text = dict[@"name"];
    return cell;
}

#pragma mark - UITableViewDelegate

@end