iOS技巧之获取本机通信录中的内容,解析通信录源代码

1、在工程中添加AddressBook.framework和AddressBookUI.framework 数组

2、获取通信录 atom

一、在infterface中定义数组并在init方法中初始化 spa

1 NSMutableArray *addressBookTemp;
2  
3 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4 {
5     addressBookTemp = [NSMutableArray array];
6 }
二、定义一个model,用来存放通信录中的各个属性

新建一个继承自NSObject的类,在.h中 .net

01 @interface TKAddressBook : NSObject {
02     NSInteger sectionNumber;
03     NSInteger recordID;
04     NSString *name;
05     NSString *email;
06     NSString *tel;
07 }
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;
13  
14 @end
在.m文件中进行synthesize
1 @implementation TKAddressBook
2 @synthesize name, email, tel, recordID, sectionNumber;
3  
4 @end

三、获取联系人 orm

在iOS6以后,获取通信录须要得到权限 继承

01     //新建一个通信录类
02     ABAddressBookRef addressBooks = nil;
03  
04     if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
05  
06     {
07         addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
08  
09         //获取通信录权限
10  
11         dispatch_semaphore_t sema = dispatch_semaphore_create(0);
12  
13         ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
14  
15         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
16  
17         dispatch_release(sema);
18  
19     }
20  
21     else
22  
23     {
24         addressBooks = ABAddressBookCreate();
25  
26     }
27  
28 //获取通信录中的全部人
29 CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
01 //通信录中人数
02 CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
03  
04 //循环,获取每一个人的我的信息
05 for (NSInteger i = 0; i < nPeople; i++)
06     {
07         //新建一个addressBook model类
08         TKAddressBook *addressBook = [[TKAddressBook alloc] init];
09         //获取我的
10         ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
11         //获取我的名字
12         CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
13         CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
14         CFStringRef abFullName = ABRecordCopyCompositeName(person);
15         NSString *nameString = (__bridge NSString *)abName;
16         NSString *lastNameString = (__bridge NSString *)abLastName;
17          
18         if ((__bridge id)abFullName != nil) {
19             nameString = (__bridge NSString *)abFullName;
20         } else {
21             if ((__bridge id)abLastName != nil)
22             {
23                 nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
24             }
25         }
26         addressBook.name = nameString;
27         addressBook.recordID = (int)ABRecordGetRecordID(person);;
28          
29         ABPropertyID multiProperties[] = {
30             kABPersonPhoneProperty,
31             kABPersonEmailProperty
32         };
33         NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
34         for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
35             ABPropertyID property = multiProperties[j];
36             ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
37             NSInteger valuesCount = 0;
38             if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
39              
40             if (valuesCount == 0) {
41                 CFRelease(valuesRef);
42                 continue;
43             }
44             //获取电话号码和email
45             for (NSInteger k = 0; k < valuesCount; k++) {
46                 CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
47                 switch (j) {
48                     case 0: {// Phone number
49                         addressBook.tel = (__bridge NSString*)value;
50                         break;
51                     }
52                     case 1: {// Email
53                         addressBook.email = (__bridge NSString*)value;
54                         break;
55                     }
56                 }
57                 CFRelease(value);
58             }
59             CFRelease(valuesRef);
60         }
61         //将我的信息添加到数组中,循环完成后addressBookTemp中包含全部联系人的信息
62         [addressBookTemp addObject:addressBook];
63          
64         if (abName) CFRelease(abName);
65         if (abLastName) CFRelease(abLastName);
66         if (abFullName) CFRelease(abFullName);
67     }
3、显示在table中
1 //行数
2 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
3     return 1;
4 }
5  
6 //列数
7 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
8     return [addressBookTemp count];
9 }
01 //cell内容
02 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
03      
04     NSString *cellIdentifier = @"ContactCell";
05      
06     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
07      
08     if (cell == nil){
09         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
10     }
11  
12     TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
13  
14     cell.textLabel.text = book.name;
15  
16     cell.detailTextLabel.text = book.tel;
17  
18     return cell;
19 }
列表效果

PS:通信录中电话号码中的"-"能够在存入数组以前进行处理,属于NSString处理的范畴,解决办法有不少种,本文很少加说明 get

相关文章
相关标签/搜索