ios获取本机通讯录中的内容,解析通讯录源代码
2013-08-26 16:42
323 查看
一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、获取通讯录
1、在infterface中定义数组并在init方法中初始化
2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自NSObject的类,在.h中
在.m文件中进行synthesize
3、获取联系人
三、显示在table中
二、获取通讯录
1、在infterface中定义数组并在init方法中初始化
NSMutableArray *addressBookTemp; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { addressBookTemp = [NSMutableArray array]; }
2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自NSObject的类,在.h中
NSMutableArray *addressBookTemp; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil @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{ addressBookTemp = [NSMutableArray array]; }
在.m文件中进行synthesize
@implementation TKAddressBook @synthesize name, email, tel, recordID, sectionNumber; @end
3、获取联系人
//新建一个通讯录类 ABAddressBookRef addressBooks = ABAddressBookCreate(); CFArrayRef allPeople = //获取通讯录中的所有人 ABAddressBookCopyArrayOfAllPeople(addressBooks); //通讯录中人数 CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks); //循环,获取每个人的个人信息 for (NSInteger i = 0; i < nPeople; i++) { //新建一个addressBook model类 TKAddressBook *addressBook = [[TKAddressBook alloc] init]; //获取个人 ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); //获取个人名字 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); } //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息 [addressBookTemp addObject:addressBook]; if (abName) CFRelease(abName); if (abLastName) CFRelease(abLastName); if (abFullName) CFRelease(abFullName); }
三、显示在table中
//行数 - (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]; } TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row]; cell.textLabel.text = book.name; cell.textLabel.text = book.name; cell.detailTextLabel.text = book.tel; return cell; }
相关文章推荐
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- [转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- 获取本机通讯录中的内容,解析通讯录源代码
- 奔五的人学iOS:swift获取网页并解析需要的内容(1)
- iOS技巧之获取本机通讯录中的内容
- iOS技巧之获取本机通讯录中的内容…
- iOS获取通讯录联系人信息
- 关于IOS获取本地通讯录信息(包含iOS9.0前后)
- 小胖说事39-----iOS 获取通讯录中联系人的所有属性
- JS 写的 HTML解析,获取文本内容
- perl post函数获取网页内容及解析数据的例子
- iOS 一个简单的获取网络json 解析成模型数组
- iOS 调用系统通讯录 获取联系人信息
- 仿IOS通讯录效果,实现获取手机通讯录、字母排序显示、搜索联系人、拨打电话
- iOS 获取通讯录中联系人的所有属性
- iOS中XML解析 (二) libxml2(实例:打印xml内容及存储到数组)