AddressBook iOS读取通讯录信息

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][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) utorelease];


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]; //将读取的某一个人信息放到我们自定义的可变数组中

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一些思路和代码示例。 首先,您需要使用文件操作函数来读取记事本中的内容,并将其存储到二叉排序树中。以下是一个示例函数,用于从文件中读取通讯录信息并将其存储到二叉排序树中: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义通讯录结构体 typedef struct AddressBook { char name[20]; char phone[20]; struct AddressBook *left; struct AddressBook *right; } AddressBook; // 从文件中读取通讯录信息并将其存储到二叉排序树中 void readAddressBookFromFile(char *filename, AddressBook **root) { FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Failed to open file: %s\n", filename); return; } char line[50]; while (fgets(line, sizeof(line), fp) != NULL) { char *name = strtok(line, ","); char *phone = strtok(NULL, ","); if (name != NULL && phone != NULL) { AddressBook *node = (AddressBook *)malloc(sizeof(AddressBook)); strcpy(node->name, name); strcpy(node->phone, phone); node->left = NULL; node->right = NULL; insertNode(root, node); } } fclose(fp); } ``` 在上面的代码中,我们使用了 `fgets` 函数逐行读取记事本中的内容,并使用 `strtok` 函数将每行内容按照逗号分隔成姓名和电话号码两个部分。然后,我们创建一个 `AddressBook` 结构体节点,并将姓名和电话号码存储到节点中。最后,我们调用 `insertNode` 函数将节点插入到二叉排序树中。 下面是一个示例的 `insertNode` 函数,用于将节点插入到二叉排序树中: ```c // 将节点插入到二叉排序树中 void insertNode(AddressBook **root, AddressBook *node) { if (*root == NULL) { *root = node; return; } if (strcmp(node->name, (*root)->name) < 0) { insertNode(&(*root)->left, node); } else { insertNode(&(*root)->right, node); } } ``` 在上面的代码中,我们使用了递归的方法将节点插入到二叉排序树中。当根节点为空时,我们直接将节点作为根节点。否则,我们比较节点的姓名和根节点的姓名,如果小于根节点的姓名,就将节点插入到左子树中;否则,就将节点插入到右子树中。 最后,您可以编写一个函数来遍历二叉排序树,并输出通讯录中的所有联系人和电话号码。以下是一个示例函数: ```c // 遍历二叉排序树并输出通讯录信息 void printAddressBook(AddressBook *root) { if (root == NULL) { return; } printAddressBook(root->left); printf("%s: %s\n", root->name, root->phone); printAddressBook(root->right); } ``` 在上面的代码中,我们使用了中序遍历的方法遍历二叉排序树,并输出每个节点的姓名和电话号码。 最后,您可以在 `main` 函数中调用上面的函数,从文件中读取通讯录信息并将其存储到二叉排序树中,然后遍历二叉排序树并输出通讯录信息。以下是一个示例 `main` 函数: ```c int main() { AddressBook *root = NULL; readAddressBookFromFile("addressbook.txt", &root); printAddressBook(root); return 0; } ``` 在上面的代码中,我们首先创建一个空的二叉排序树,并调用 `readAddressBookFromFile` 函数从文件中读取通讯录信息并将其存储到二叉排序树中。然后,我们调用 `printAddressBook` 函数遍历二叉排序树并输出通讯录信息

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code科技狂热者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值