iOS 访问通讯录

1.要 导入系统的 2个包

//

//  TheAddressBook.m

//  Message

//

//  Created by bml on 14-6-3.

//  Copyright (c) 2014 bml. All rights reserved.

//


#import "TheAddressBook.h"

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

#import "MyCell.h"

@interface TheAddressBook ()

{

    int selectIndex;

}

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (nonatomic,strong)NSMutableArray *phoneName;

@property (nonatomic,strong)NSMutableArray *phoneNum;

@property (nonatomic,strong)NSMutableArray *array;

@end


@implementation TheAddressBook


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    selectIndex =-1;

    //    NSString *personName22 = nil;

    

    //    NSString *name =@"0.0";

    //    name=[personName stringByAppendingString:personName];

    

    

    self.phoneName = [NSMutableArray new];

    self.phoneNum  = [NSMutableArray new];

    

    

    ABAddressBookRef addressBook = nil;

    //判断版本

    if ([UIDevice currentDevice].systemVersion.floatValue>=6.0) {

        addressBook=ABAddressBookCreateWithOptions(NULL, NULL);

        

        dispatch_semaphore_t sema=dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)

                                                 {

                                                     dispatch_semaphore_signal(sema);

                                                 });

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

        //dispatch_release(sema);

    }

    else

    {

        addressBook=ABAddressBookCreateWithOptions(NULL, NULL);

    }

    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);

    

    for(int i = 0; i < CFArrayGetCount(results); i++)

    {

        ABRecordRef person = CFArrayGetValueAtIndex(results, i);

        //读取firstname

        //读取电话多值

        ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);

        for (int k = 0; k<ABMultiValueGetCount(phone); k++)

        {

            //获取电话Label

            // NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));

            //获取該Label下的电话值

            NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);

            

            //获取姓

            NSString *personName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

            //获取名

            NSString *lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

            //将姓名组合起来

            

            //            name=[personName stringByAppendingString:lastname];

            if (personName !=nil)

            {

                

                [self.phoneName addObject:personName];

                NSLog(@" phoneName is :%@",self.phoneName);

            }

            if (lastname !=nil) {

                [self.phoneName addObject:lastname];

                

            }

            //            NSLog(@"name is value --->%@",name);

            //            NSLog(@"%@",personPhone);

            //            NSLog(@"%@",personName);

            //            NSLog(@"%@",lastname);

            //            [self.phoneName addObject:personName];

            //            [self.phoneNum addObject:personPhone];

            //              [self.phoneName addObject:personName];

            NSLog(@" phoneName is :%@",self.phoneName);

            

            

            

        }

        //        [self.array addObject:self.phoneName];

    }

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

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

{

    NSLog(@"self.phoneName.count is :%zi",self.phoneName.count);

    return self.phoneName.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellIdentfier = @"cell";

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentfier forIndexPath:indexPath];

    cell.topTile.text = [self.phoneName objectAtIndex:indexPath.row];

    //    cell.subTutle.text = [self.phoneName objectAtIndex:indexPath.row];

    cell.btn1.selected =NO;

    [cell.btn1 addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];

    

    //    if (cell.btn1.selected ==) {

    //        <#statements#>

    //    }

    [cell.btn1 setTag:indexPath.row];

    return cell;

}

-(void)onClick:(UIButton *)sender

{

    int row = sender.tag;

    //    MyCell *cell = (MyCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];

    //    if (sender.selected) {

    //        sender.selected = NO;

    //        NSLog(@"第二次相应事件");

    //

    //        [cell.btn2 setHidden:NO];

    //

    //    }else {

    //        sender.selected = YES;

    //        [cell.btn2 setHidden:YES];

    //        NSLog(@"第一次相应事件");

    

    

    //    }

    NSLog(@"cell is btn ---->%zi",row);

    

    if (selectIndex != row) {

        NSLog(@"selectIndex  is  value :%zi",selectIndex);

        if (selectIndex != -1) {

            MyCell *cell = (MyCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:selectIndex inSection:0]];

            [cell.btn2 setHidden:NO];

            //            [cell.btn setImage:[UIImage imageNamed:@"box_chenck"] forState:UIControlStateNormal];

        }

        

        selectIndex = row;

        MyCell *cell = (MyCell *)[self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];

        [cell.btn2 setHidden:YES];

        //        [cell.btn setImage:[UIImage imageNamed:@"box_chencked"] forState:UIControlStateNormal];

        //        ToPublishCircleBean *bean3 = [self.cellData objectAtIndex:row];

        //        self.string= bean3.title;

    }

    

    

    

}

/*

 #pragma mark - Navigation

 

 // In a storyboard-based application, you will often want to do a little preparation before navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

 {

 // Get the new view controller using [segue destinationViewController].

 // Pass the selected object to the new view controller.

 }

 */


@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值