- 浏览: 29596 次
- 性别:
- 来自: 北京
最新评论
iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和AddressBook两个包,具体实现方法如下
////////////////////////////////////////////////////////////////////////////
//定义通讯录名字为addressbook
ABAddressBookRef addressBook = ABAddressBookCreate();
//将通讯录中的信息用数组方式读出
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
//获取通讯录中联系人
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
iphoneContactList = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < nPeople; i++)
{
IphoneContact * iphoneContact = [[IphoneContact alloc] init];
NSData *imageData = [[[NSData alloc] init]autorelease];
NSString *address = [[[NSString alloc] init]autorelease];
ABRecordRef person = CFArrayGetValueAtIndex(contacts, i);//取出某一个人的信息
NSInteger lookupforkey =(NSInteger)ABRecordGetRecordID(person);//读取通讯录中联系人的唯一标识
NSDate * createDate = (NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty);// 读取通讯录中联系人的创建日期
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString* birthDay = [formatter stringFromDate:createDate];
[formatter release];
NSString *createDate1 = [birthDay stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *createDate2 = [createDate1 stringByReplacingOccurrencesOfString:@":" withString:@""];
NSString *createDate3 = [createDate2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"1111========%@",createDate3);
NSLog(@"222222========%@",birthDay);
NSString *indexInIphone = [NSString stringWithFormat:@"%i",lookupforkey];
iphoneContact.lookUpKey = [createDate3 stringByAppendingString:indexInIphone];
//上诉操作是将某个联系人的标识号与创建日期进行组合,得到唯一的标识,是为了当时特殊的需要,一般不会有这种变态应用,这是因为ABRecordGetRecordID在一个手机中是唯一的,即使删掉某一个联系人,这个号也不会在被占用。
//读取联系人姓名属性
if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))== nil) {
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
}else if (ABRecordCopyValue(person, kABPersonLastNameProperty) == nil&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
}else if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
NSString *first =(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *last = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
iphoneContact.contactName = [NSString stringWithFormat:@"%@%@",last,first];
}
//读取联系人姓名的第一个汉语拼音,用于排序,调用此方法需要在程序中加载pingyin.h pingyin.c,如有需要请给我联系。
iphoneContact.pinyin =[[NSString stringWithFormat:@"%c",pinyinFirstLetter([iphoneContact.contactName characterAtIndex:0])] uppercaseString];
//读取联系人公司信息
iphoneContact.contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
//读取联系人工作
iphoneContact.contactJob = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
//读取联系人email信息,email有好几种,比如工作邮箱,家庭邮箱,通过ABMultiValueCopyLabelAtIndex 属性来判断,下面给出了例子
ABMultiValueRef emailForWORK = ABRecordCopyValue(person, kABPersonEmailProperty);
if ((emailForWORK != nil)&&ABMultiValueGetCount(emailForWORK)>0) {
for (int k = 0; k < ABMultiValueGetCount(emailForWORK); k++) {
NSString * aEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailForWORK, k);
NSString * aEmailLabel = (NSString *)ABMultiValueCopyLabelAtIndex(emailForWORK, k);
if ([aEmailLabel isEqualToString:@"_$!<Work>!$_"]) {
iphoneContact.contactEmail = aEmail;
}
}
}
//读取通信地址信息,在ios中国家,省份,城市,区,街道,号都是单独存储的,需要单独读取然后在组合(根据需要)
ABMultiValueRef addressTotal = ABRecordCopyValue(person, kABPersonAddressProperty);
if (addressTotal) {
int count = ABMultiValueGetCount(addressTotal);
for(int j = 0; j < count; j++)
{
NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(addressTotal, j);
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
if(country != nil)
address = [NSString stringWithFormat:@"%@",country];
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
if(city != nil)
address = [address stringByAppendingFormat:@"%@",city];
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
if(state != nil)
address = [address stringByAppendingFormat:@"%@",state];
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
if(street != nil)
address = [address stringByAppendingFormat:@"%@",street];
}
iphoneContact.contactAdress = address;
}
//读取电话信息,和emial类似,也分为工作电话,家庭电话,工作传真,家庭传真。。。。
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
if ((phone != nil)&&ABMultiValueGetCount(phone)>0) {
for (int m = 0; m < ABMultiValueGetCount(phone); m++) {
NSString * aPhone = [(NSString *)ABMultiValueCopyValueAtIndex(phone, m) autorelease];
NSString * aLabel = [(NSString *)ABMultiValueCopyLabelAtIndex(phone, m) autorelease];
if ([aLabel isEqualToString:@"_$!<Mobile>!$_"]) {
iphoneContact.contactMobile= aPhone;
}
if ([aLabel isEqualToString:@"_$!<WorkFAX>!$_"]) {
iphoneContact.contactFax = aPhone;
}
if ([aLabel isEqualToString:@"_$!<Work>!$_"]) {
iphoneContact.contactTelephone= aPhone;
}
}
}
//读取照片信息
imageData = (NSData *)ABPersonCopyImageData(person);
UIImage *myImage = [UIImage imageWithData:imageData];
CGSize newSize = CGSizeMake(55, 55);
UIGraphicsBeginImageContext(newSize);
[myImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();//上诉代码实现图片压缩,可根据需要选择,现在是压缩到55*55
imageData = UIImagePNGRepresentation(newImage);
if (imageData) {
NSData * BimageData = [GTMBase64 encodeData:imageData];
iphoneContact.headImage= [[NSString alloc] initWithData:BimageData encoding:NSUTF8StringEncoding];
}
iphoneContact.isSelected = NO;
[iphoneContactList addObject:iphoneContact]; //将读取的某一个人信息放到我们自定义的可变数组中
}
////////////////////////////////////////////////////////////////////////////
//定义通讯录名字为addressbook
ABAddressBookRef addressBook = ABAddressBookCreate();
//将通讯录中的信息用数组方式读出
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
//获取通讯录中联系人
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
iphoneContactList = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < nPeople; i++)
{
IphoneContact * iphoneContact = [[IphoneContact alloc] init];
NSData *imageData = [[[NSData alloc] init]autorelease];
NSString *address = [[[NSString alloc] init]autorelease];
ABRecordRef person = CFArrayGetValueAtIndex(contacts, i);//取出某一个人的信息
NSInteger lookupforkey =(NSInteger)ABRecordGetRecordID(person);//读取通讯录中联系人的唯一标识
NSDate * createDate = (NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty);// 读取通讯录中联系人的创建日期
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString* birthDay = [formatter stringFromDate:createDate];
[formatter release];
NSString *createDate1 = [birthDay stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *createDate2 = [createDate1 stringByReplacingOccurrencesOfString:@":" withString:@""];
NSString *createDate3 = [createDate2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"1111========%@",createDate3);
NSLog(@"222222========%@",birthDay);
NSString *indexInIphone = [NSString stringWithFormat:@"%i",lookupforkey];
iphoneContact.lookUpKey = [createDate3 stringByAppendingString:indexInIphone];
//上诉操作是将某个联系人的标识号与创建日期进行组合,得到唯一的标识,是为了当时特殊的需要,一般不会有这种变态应用,这是因为ABRecordGetRecordID在一个手机中是唯一的,即使删掉某一个联系人,这个号也不会在被占用。
//读取联系人姓名属性
if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))== nil) {
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
}else if (ABRecordCopyValue(person, kABPersonLastNameProperty) == nil&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
}else if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
NSString *first =(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *last = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
iphoneContact.contactName = [NSString stringWithFormat:@"%@%@",last,first];
}
//读取联系人姓名的第一个汉语拼音,用于排序,调用此方法需要在程序中加载pingyin.h pingyin.c,如有需要请给我联系。
iphoneContact.pinyin =[[NSString stringWithFormat:@"%c",pinyinFirstLetter([iphoneContact.contactName characterAtIndex:0])] uppercaseString];
//读取联系人公司信息
iphoneContact.contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
//读取联系人工作
iphoneContact.contactJob = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
//读取联系人email信息,email有好几种,比如工作邮箱,家庭邮箱,通过ABMultiValueCopyLabelAtIndex 属性来判断,下面给出了例子
ABMultiValueRef emailForWORK = ABRecordCopyValue(person, kABPersonEmailProperty);
if ((emailForWORK != nil)&&ABMultiValueGetCount(emailForWORK)>0) {
for (int k = 0; k < ABMultiValueGetCount(emailForWORK); k++) {
NSString * aEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailForWORK, k);
NSString * aEmailLabel = (NSString *)ABMultiValueCopyLabelAtIndex(emailForWORK, k);
if ([aEmailLabel isEqualToString:@"_$!<Work>!$_"]) {
iphoneContact.contactEmail = aEmail;
}
}
}
//读取通信地址信息,在ios中国家,省份,城市,区,街道,号都是单独存储的,需要单独读取然后在组合(根据需要)
ABMultiValueRef addressTotal = ABRecordCopyValue(person, kABPersonAddressProperty);
if (addressTotal) {
int count = ABMultiValueGetCount(addressTotal);
for(int j = 0; j < count; j++)
{
NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(addressTotal, j);
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
if(country != nil)
address = [NSString stringWithFormat:@"%@",country];
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
if(city != nil)
address = [address stringByAppendingFormat:@"%@",city];
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
if(state != nil)
address = [address stringByAppendingFormat:@"%@",state];
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
if(street != nil)
address = [address stringByAppendingFormat:@"%@",street];
}
iphoneContact.contactAdress = address;
}
//读取电话信息,和emial类似,也分为工作电话,家庭电话,工作传真,家庭传真。。。。
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
if ((phone != nil)&&ABMultiValueGetCount(phone)>0) {
for (int m = 0; m < ABMultiValueGetCount(phone); m++) {
NSString * aPhone = [(NSString *)ABMultiValueCopyValueAtIndex(phone, m) autorelease];
NSString * aLabel = [(NSString *)ABMultiValueCopyLabelAtIndex(phone, m) autorelease];
if ([aLabel isEqualToString:@"_$!<Mobile>!$_"]) {
iphoneContact.contactMobile= aPhone;
}
if ([aLabel isEqualToString:@"_$!<WorkFAX>!$_"]) {
iphoneContact.contactFax = aPhone;
}
if ([aLabel isEqualToString:@"_$!<Work>!$_"]) {
iphoneContact.contactTelephone= aPhone;
}
}
}
//读取照片信息
imageData = (NSData *)ABPersonCopyImageData(person);
UIImage *myImage = [UIImage imageWithData:imageData];
CGSize newSize = CGSizeMake(55, 55);
UIGraphicsBeginImageContext(newSize);
[myImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();//上诉代码实现图片压缩,可根据需要选择,现在是压缩到55*55
imageData = UIImagePNGRepresentation(newImage);
if (imageData) {
NSData * BimageData = [GTMBase64 encodeData:imageData];
iphoneContact.headImage= [[NSString alloc] initWithData:BimageData encoding:NSUTF8StringEncoding];
}
iphoneContact.isSelected = NO;
[iphoneContactList addObject:iphoneContact]; //将读取的某一个人信息放到我们自定义的可变数组中
}
发表评论
-
MFMessageComposeViewController发送短信
2013-04-11 13:33 1235MFMessageComposeViewController ... -
RESTful HTTP的实践
2013-04-11 12:20 978本文对RESTful HTTP的基础原理做了一个概览,探讨了开 ... -
IOS使用第三方工具(RegexKitLite)实现正则表达式
2013-04-10 18:19 1816在IOS应用中,经常要输入数据,然输入数据,就要 ... -
iPhone:动态获取UILabel的高度和宽度
2013-04-09 18:10 767在使用UILabel存放字符串时,经常需要获取label的长宽 ... -
获得通讯录中联系人的所有属性ABAddressBookRef
2013-04-09 13:11 976获得通讯录中联系人的所有属性 ,看代码: ABA ... -
iOS 绘制直线、矩形、文字的方式
2013-04-08 10:35 4247首先,获取上下文 CGContextRef context = ... -
iOS 开发小技巧(二)
2013-04-08 09:36 6651、获取全局的Delegate对象,这样我们可以调用这个对象里 ... -
iOS 开发小技巧(一)
2013-04-08 09:27 6451.取字符串中的某段字符串 [plain] view pla ... -
IOS导航栏的创建方法
2013-04-07 15:45 972基本思路是: 1.创建一个导航栏(UINavigationB ... -
iOS 绘制UIImage的方式
2013-04-07 13:03 2363一:在绘制到context前通过矩阵垂直翻转坐标系 // ui ... -
理解可变参数va_list、va_start、va_arg、va_end原理及使用方法
2013-04-07 12:59 1705在VC++6.0的include有一个stdarg.h头文件, ... -
iOS 开发小技巧(三)
2013-04-06 09:21 6361.判断邮箱格式是否正确的代码 //利用正则表达式验证 -(B ...
相关推荐
这是本人自学以后写的一个独立于IOS自带AddressBook的通讯录小项目,实现了登陆、显示联系人、删除联系人、增加联系人、修改联系人、编辑联系人(可编辑姓名、电话、头像),并能将联系人备份到IOS联系人以及从IOS...
网上通讯录 addressBook网上通讯录 addressBook网上通讯录 addressBook网上通讯录 addressBook网上通讯录 addressBook网上通讯录 addressBook
ZMAddressBook:iOS通讯录操作, 对通讯录进行添加,删除,修改,查询等操作.
AddressBook,IOS,通讯录
获取系统通讯录的名字和电话【srxboys】 兼容 iOS6 / 7/8 等等 所有版本 可以看淘宝 收货地址
前阵子在做开发的时候用到了手机通讯录功能,在ios9.0以上的系统中AddressBook会直接崩溃掉,后来发现原来是苹果在iOS9.0之后通讯录用的是Contacts.framework,从网上找了一些demo,优化了下效果,做了下适配,在ios...
自己写的一个获取本地通讯录,根据汉字的拼音进行录排序和添加了索引
iOS 获取手机通讯录的demo,很方便集成到项目,几行代码就可轻松调用 AddressBook *addressBook = [AddressBook shareAddressBook]; addressBook.delegate = self; [addressBook ...
最近学习了iOS获取手机通讯录方式方法,现在分享给大家。希望此文章对各位有所帮助。 一、iOS 9 以前的通讯录框架 AddressBookUI框架:提供了联系人列表界面、联系人详情界面、添加联系人界面等,一般用于选择联系...
jap网络通讯录addressBook
本文实例为大家分享了iOS获取通讯录的4种方式,供大家参考,具体内容如下 使用场景 一些App通过手机号码来推荐好友,如 微博、支付宝 首先客户端会获取通讯录中的所有手机号然后将这些手机号提交到App服务器中,...
iOS 获取系统通讯录的名字和电话【srxboys】【第二版】 同时兼容 iOS6 / 7/8/9/10 等等 所有版本 可以看淘宝 收货地址 亲测 实现 > iOS6 代码例子: //初始化对象 在viewDidLoad __weak typeof(self)weakSelf = ...
java和数据库开发的web通讯录系统,可以实现联系人的添加,修改删除等
地址簿 命令行通讯录 使用JavaScript和Node.js构建命令行通讯簿
地址簿公司通讯录项目源码(ios入门教程)
addressBook用于获取iOS 通讯录,可以修改 增加 删除查询通讯录
通过字典和可变数组嵌套的方法实现了通讯录的功能,包括增删查和排序功能
PPGetAddressBook对AddressBook框架(iOS9之前)和Contacts框架(iOS9之后)做了对应的封装处理; 支持获取按联系人姓名首字拼音A~Z排序(重点:已经对姓名的第二个字做了处理,排序更准确!);
该项目可帮助用户处理地址管理。 它包括以下功能:“添加新联系人”,“删除联系人”,“搜索联系人”,“按名字/姓氏排序联系人”和“查看所有联系人”。 它使用以下Java包:import java.awt。...
Corporate Addressbook GAL是指the Global Address List 意为全球联系人清单 是存在公司Exchange服务器上的联系人信息 你可以利用这款软件在Android安卓手机上查找别人的联系方式 或者将其保存到自己的本地通讯录中 ...