一思喊、在工程中添加AddressBook.framework
和AddressBookUI.framework
二懂牧、獲取通訊錄
1核无、在infterface中定義數(shù)組并在init方法中初始化
NSMutableArray *addressBookTemp;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
addressBookTemp = [NSMutableArray array];
}
2,定義一個(gè) model, 用來存放通訊錄中的各個(gè)屬性
新建一個(gè)集成子 NSOject
的類,在.h
中.
@interface TKAddressBook : NSObject {
NSInteger sectionNumber;
NSInteger recordID;
NSString *name;
NSString *email;
NSString *tel;
}
@property NSInteger sectionNumber;
@property NSInteger recordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;
@end
在.m文件中進(jìn)行synthesize
@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
@end
3.獲取聯(lián)系人
在iOS6之后,獲取通訊錄需要獲得權(quán)限
//新建一個(gè)通訊錄類
ABAddressBookRef addressBooks = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) { addressBooks = ABAddressBookCreateWithOptions(NULL, NULL);
//獲取通訊錄權(quán)限
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);
dispatch_release(sema);
} else {
addressBooks = ABAddressBookCreate();
}
//獲取通訊錄中的所有人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
//通訊錄中人數(shù)
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
//循環(huán),獲取每個(gè)人的個(gè)人信息
for (NSInteger i = 0; i < nPeople; i++){
//新建一個(gè)addressBook model類
TKAddressBook *addressBook = [[TKAddressBook alloc] init];
//獲取個(gè)人
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//獲取個(gè)人名字
CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
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);
}
//將個(gè)人信息添加到數(shù)組中蚁阳,循環(huán)完成后addressBookTemp中包含所有聯(lián)系人的信息
[addressBookTemp addObject:addressBook];
if (abName) CFRelease(abName);
if (abLastName) CFRelease(abLastName);
if (abFullName) CFRelease(abFullName);
三、顯示在table中
//行數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//列數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [addressBookTemp count];
}
//cell內(nèi)容
- (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]; }
TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
cell.textLabel.text = book.name;
cell.detailTextLabel.text = book.tel;
return cell;
}
列表效果