【我就看看不说话】读取通讯录
来源:互联网 发布:西门子plc编程实例精解 编辑:程序博客网 时间:2024/04/24 21:30
一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、获取通讯录
1、在infterface中定义数组并在init方法中初始化
NSMutableArray *addressBookTemp;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
addressBookTemp = [NSMutableArray array];
}
2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自NSObject的类,在.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文件中进行synthesize
@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
@end
3、获取联系人
在iOS6之后,获取通讯录需要获得权限
//新建一个通讯录类
ABAddressBookRef addressBooks =nil;
if([[UIDevice currentDevice].systemVersion floatValue] >=6.0)
{
addressBooks = ABAddressBookCreateWithOptions(NULL,NULL);
//获取通讯录权限
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBooks, ^(boolgranted, CFErrorRef error){dispatch_semaphore_signal(sema);});
dispatch_semaphore_wait(sema,DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
else
{
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((__bridgeid)abFullName != nil) {
nameString = (__bridge NSString *)abFullName;
}else{
if((__bridgeid)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) {
case0: {// Phone number
addressBook.tel = (__bridge NSString*)value;
break;
}
case1: {// 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 {
return1;
}
//列数
- (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.detailTextLabel.text = book.tel;
returncell;
}
列表效果
- 【我就看看不说话】读取通讯录
- 【我就看看不说话】NSAttributedString
- 【我就看看不说话】UIActivity
- 【我就看看不说话】 UIActionSheet
- 【我就看看不说话】UIAlertView
- 【我就看看不说话】UIAppearance
- 【我就看看不说话】UITextField
- 【我就看看不说话】 svn
- 【我就看看不说话】常用函数
- 【我就看看不说话】Block 使用
- 【我就看看不说话】NSDate 日期
- 【我就看看不说话】nsstring 使用
- 【我就看看不说话】xcode 快捷键
- 【我就看看不说话】有用网站
- 【我就看看不说话】ios 指南
- 【我就看看不说话】自动布局
- 【我就看看不说话】UITextView NSTextContainer NSLayoutManager NSTextStorage
- 【我就看看不说话】证书的制作
- APUE——信号发送函数kill,raise,pause,alarm
- Oracle创建表空间、创建用户以及授权、查看权限
- 命令行操作<1>
- java AES 加密与解密
- linux中fork()函数详解(原创!!实例讲解)
- 【我就看看不说话】读取通讯录
- 线程同步的三种方式
- 第十一周 项目四:(1)类族的设计
- 关于本技术博客的说明
- android绘制进度条
- Java单例模式
- 解题报告 之 ZOJ3861 Valid Pattern Lock
- 图论算法 有图有代码 万字总结 向前辈致敬
- uva 10313 Pay the Price (DP)