iOS技巧之获取本机通讯录中的内容

一、在工程中添加AddressBook.framework和AddressBookUI.framework

获取本机通讯录中的内容,显示在列表(table)中, iOS6之后,苹果对系统中通讯录日历等控件的调用进行了权限控制,获取通讯录需加上请求权限部分的代码

firstViewController.h中

加入AddressBook/AddressBook.h,AddressBookUI/AddressBookUI.h头文件再加入UITableView的两个协议

#import <UIKit/UIKit.h>

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

@interface firstViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

    NSMutableArray *addressBookTemp;

}

@property (nonatomic,retain)UITableView *tableView_;

@end

firstViewController.m中

#import "firstViewController.h"

#import "shuxing.h"

@interface firstViewController ()

@end

@implementation firstViewController

@synthesize tableView_;

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

{

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

    if (self) {

        addressBookTemp = [NSMutableArray array];

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];


    UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds];

    table.delegate = self;

    table.dataSource = self;

   [self.view addSubview:table];

    ABAddressBookRef addressBooks = nil;

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

        addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);

        //获取通讯录权限

        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);


    }

    else

    {

        addressBooks = ABAddressBookCreate();

    }

    //获取通讯录中的所有人

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);

    //通讯录中人数

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);

    for(NSInteger i = 0; i<nPeople; i++)

    {

        //创建一个addressBook shuxing

        shuxing *addressBook = [[shuxing alloc]init];

        //获取个人

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //获取个人名字

        CFTypeRef abName = ABRecordCopyValue(person, kABPersonAddressProperty);

        CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

        CFStringRef abFullName = ABRecordCopyCompositeName(person);

        NSString *nameString = (__bridge NSString *)abName;

        NSString *lastNameString = (__bridge NSString *)abLastName;

        if ((__bridge id)abFullName != nil) {            

            nameString = (__bridge NSString *)abFullName;

        } else {

            

            if ((__bridge id)abLastName != nil)

                

            {               

                nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];

            }

        }     

        addressBook.name = nameString;       

        addressBook.recordID = (int)ABRecordGetRecordID(person);;

        ABPropertyID multiProperties[] = {

            kABPersonPhoneProperty,

            kABPersonEmailProperty

        };

        NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);

        

        for (NSInteger j = 0; j < multiPropertiesTotal; j++) {

        

            ABPropertyID property = multiProperties[j];

            ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);

            NSInteger valuesCount = 0;

            if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);


            if (valuesCount == 0) {

                CFRelease(valuesRef);

                continue;

            }

            //获取电话号码和email

            for (NSInteger k = 0; k < valuesCount; k++) {

            CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);

                switch (j) {

                    case 0: {// Phone number

                        addressBook.tel = (__bridge NSString*)value;

                        break;

                    }

                    case 1: {// Email

                        addressBook.email = (__bridge NSString*)value;

                        break;

                    }

                }

                CFRelease(value);

            }

            CFRelease(valuesRef);

        }

        //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息

        [addressBookTemp addObject:addressBook];   

            }

}


//行数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

//列数

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

    return [addressBookTemp count];

}

//cell内容

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

    NSString *cellIdentifier = @"ContactCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }

    shuxing *book = [addressBookTemp objectAtIndex:indexPath.row];

    cell.textLabel.text = book.name;

    cell.detailTextLabel.text = book.tel;

     return cell;

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

@end


shuxing.h

#import <Foundation/Foundation.h>


@interface shuxing : NSObject

    

@property NSInteger sectionNumber;

@property NSInteger recordID;

@property(nonatomic,retain)NSString *name;

@property(nonatomic,retain)NSString *email;

@property(nonatomic,retain)NSString *tel;

@end

shuxing.m

#import "shuxing.h"

@implementation shuxing

@synthesize name,email,tel,recordID,sectionNumber;

@end




iOS技巧之获取本机通讯录中的内容,布布扣,bubuko.com

iOS技巧之获取本机通讯录中的内容

上一篇:Android Canvas练习(10)自已绘南丁格尔玫瑰图(Nightingale rose diagram)


下一篇:苹果数据线充电线损坏自己修