使用ABAddressBook过滤已知电话号码的用户联系人列表

时间:2014-07-16 20:43:05

标签: ios objective-c abaddressbook

无论出于何种原因,我无法在任何地方找到这个,但我正在编写一个iphone应用程序,我正在尝试 使用ABAddressBook框架过滤已知电话号码的用户联系人列表。

我希望将联系人列表(UITableViewController)拆分为两个部分:应用程序上已有联系人,应用程序上没有联系人。我将联系人列表存储在NSArray中,我需要弄清楚如何确定数组中的联系人是否已经在应用程序中。

完成此操作后,我需要能够将已使用应用部分的联系人添加到“朋友列表”UITableViewController

非常感谢你!

这是我的.m文件代码:

    - (void)viewDidLoad
    {
[super viewDidLoad];

self.contacts = [[NSArray alloc]init];
[self getAllContacts];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

//GETS RID OF WEIRD OVERLAP BUG ON TABLE VIEW CONTROLLER
[self.navigationController.view setBackgroundColor:[UIColor whiteColor]];
    }

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return self.contacts.count;
}

-(void)getAllContacts
{
APAddressBook *addressBook = [[APAddressBook alloc]init];
addressBook.fieldsMask = APContactFieldAll;
addressBook.filterBlock = ^BOOL(APContact *contact){
    return (contact.phones.count > 0) && !(contact.firstName == nil && contact.lastName     == nil);
};

[addressBook loadContacts:^(NSArray *apContacts, NSError *error) {
    if(!error){
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        for (APContact *AP in apContacts) {
            Contact *contact = [Contact new];
            contact.firstName = AP.firstName?[AP.firstName stringByAppendingString:@"   "]:@"";
            contact.lastName = AP.lastName?AP.lastName:@" ";
            contact.fullName = [contact.firstName   stringByAppendingString:contact.lastName];
            if(AP.emails && [AP.emails count] > 0) {
                contact.email = [AP.emails firstObject]?:nil;
            }

            if(AP.thumbnail ) {
                contact.image = AP.thumbnail;
            } else {
              /*  contact.image = [UIImage imageNamed:]; ADD GENERIC DEFAULT IMAGE*/
            }
            if(AP.phones && [AP.phones count] > 0) {
                NSString *phone = [AP.phones firstObject];
                contact.phoneNumber = [NSString formatNumber:phone];
                contact.numberToDisplay = phone;
                contact.identifier = [NSString formatNumber:phone];
            } /*else {
                contact.identifier = [AP.emails firstObject];

        }*/
            [dic setValue:contact forKey:contact.fullName];
        }

        self.contacts = [dic allValues];

        [self setupContacts];

    } else {
        //write an alert error.description
    }


 }];

}

-(void)setupContacts{

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:@"fullName"   ascending:YES selector:@selector(caseInsensitiveCompare:)];

NSArray *descriptors = [NSArray arrayWithObject:descriptor];
self.contacts = [self.contacts sortedArrayUsingDescriptors:descriptors];

if (!self.selectedContacts) {
    self.selectedContacts = [[NSMutableDictionary  alloc]initWithCapacity:self.contacts.count];
}
self.tableView.allowsSelection = YES;
self.tableView.allowsMultipleSelection = YES;

 [self.tableView reloadData];

}


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

// Configure the cell...
if (cell == nil) {
    cell = (ContactsCell *)[[ContactsCell alloc]  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

NSInteger row = indexPath.row;
Contact *contact = [self.contacts objectAtIndex:row];
cell.mainTextLabel.text = contact.fullName;

cell.hidden = NO;

return cell;
}

0 个答案:

没有答案