一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、获取通讯录
1、在infterface中定义数组并在init方法中初始化
1 | NSMutableArray *addressBookTemp; |
3 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
5 | addressBookTemp = [NSMutableArray array]; |
2、定义一个model,用来存放通讯录中的各个属性 新建一个继承自NSObject的类,在.h中
01 | @interface TKAddressBook : NSObject { |
02 | NSInteger sectionNumber; |
08 | @property NSInteger sectionNumber; |
09 | @property NSInteger recordID; |
10 | @property (nonatomic, retain) NSString *name; |
11 | @property (nonatomic, retain) NSString *email; |
12 | @property (nonatomic, retain) NSString *tel; |
在.m文件中进行synthesize
1 | @implementation TKAddressBook |
2 | @synthesize name, email, tel, recordID, sectionNumber; |
3、获取联系人
02 | ABAddressBookRef addressBooks = ABAddressBookCreate(); |
03 | CFArrayRef allPeople = |
06 | ABAddressBookCopyArrayOfAllPeople(addressBooks); |
09 | CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks); |
12 | for (NSInteger i = 0; i < nPeople; i++) |
15 | TKAddressBook *addressBook = [[TKAddressBook alloc] init]; |
17 | ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); |
19 | CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty); |
20 | CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty); |
21 | CFStringRef abFullName = ABRecordCopyCompositeName(person); |
22 | NSString *nameString = (__bridge NSString *)abName; |
23 | NSString *lastNameString = (__bridge NSString *)abLastName; |
25 | if ((__bridge id)abFullName != nil) { |
26 | nameString = (__bridge NSString *)abFullName; |
28 | if ((__bridge id)abLastName != nil) |
30 | nameString = [NSString stringWithFormat:@ "%@ %@" , nameString, lastNameString]; |
33 | addressBook.name = nameString; |
34 | addressBook.recordID = ( int )ABRecordGetRecordID(person);; |
36 | ABPropertyID multiProperties[] = { |
37 | kABPersonPhoneProperty, |
38 | kABPersonEmailProperty |
40 | NSInteger multiPropertiesTotal = sizeof (multiProperties) / sizeof (ABPropertyID); |
41 | for (NSInteger j = 0; j < multiPropertiesTotal; j++) { |
42 | ABPropertyID property = multiProperties[j]; |
43 | ABMultiValueRef valuesRef = ABRecordCopyValue(person, property); |
44 | NSInteger valuesCount = 0; |
45 | if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef); |
47 | if (valuesCount == 0) { |
52 | for (NSInteger k = 0; k < valuesCount; k++) { |
53 | CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k); |
56 | addressBook.tel = (__bridge NSString*)value; |
60 | addressBook.email = (__bridge NSString*)value; |
69 | [addressBookTemp addObject:addressBook]; |
71 | if (abName) CFRelease(abName); |
72 | if (abLastName) CFRelease(abLastName); |
73 | if (abFullName) CFRelease(abFullName); |
三、显示在table中
2 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
7 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8 | return [addressBookTemp count]; |
02 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
04 | NSString *cellIdentifier = @ "ContactCell" ; |
06 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; |
09 | cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; |
12 | TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row]; |
14 | cell.textLabel.text = book.name; |
16 | cell.detailTextLabel.text = book.tel; |