1、在工程中添加AddressBook.framework和AddressBookUI.framework 數組
2、獲取通信錄 atom
一、在infterface中定義數組並在init方法中初始化 spa
1 |
NSMutableArray *addressBookTemp; |
3 |
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
5 |
addressBookTemp = [NSMutableArray array]; |
二、定義一個model,用來存放通信錄中的各個屬性
新建一個繼承自NSObject的類,在.h中 .net
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; |
三、獲取聯繫人 orm
在iOS6以後,獲取通信錄須要得到權限 繼承
02 |
ABAddressBookRef addressBooks = nil; |
04 |
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) |
07 |
addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); |
11 |
dispatch_semaphore_t sema = dispatch_semaphore_create(0); |
13 |
ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);}); |
15 |
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); |
17 |
dispatch_release(sema); |
24 |
addressBooks = ABAddressBookCreate(); |
29 |
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks); |
02 |
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks); |
05 |
for (NSInteger i = 0; i < nPeople; i++) |
07 |
//新建一個addressBook model類 |
08 |
TKAddressBook *addressBook = [[TKAddressBook alloc] init]; |
10 |
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); |
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; |
18 |
if ((__bridge id)abFullName != nil) { |
19 |
nameString = (__bridge NSString *)abFullName; |
21 |
if ((__bridge id)abLastName != nil) |
23 |
nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString]; |
26 |
addressBook.name = nameString; |
27 |
addressBook.recordID = (int)ABRecordGetRecordID(person);; |
29 |
ABPropertyID multiProperties[] = { |
30 |
kABPersonPhoneProperty, |
31 |
kABPersonEmailProperty |
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); |
40 |
if (valuesCount == 0) { |
45 |
for (NSInteger k = 0; k < valuesCount; k++) { |
46 |
CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k); |
48 |
case 0: {// Phone number |
49 |
addressBook.tel = (__bridge NSString*)value; |
53 |
addressBook.email = (__bridge NSString*)value; |
61 |
//將我的信息添加到數組中,循環完成後addressBookTemp中包含全部聯繫人的信息 |
62 |
[addressBookTemp addObject:addressBook]; |
64 |
if (abName) CFRelease(abName); |
65 |
if (abLastName) CFRelease(abLastName); |
66 |
if (abFullName) CFRelease(abFullName); |
3、顯示在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; |
列表效果
PS:通信錄中電話號碼中的"-"能夠在存入數組以前進行處理,屬於NSString處理的範疇,解決辦法有不少種,本文很少加說明 get