您的位置:首页 > 其它

iphone学习笔记--获得iPhone通讯录中所有联系人的电话号码和邮箱

2011-10-14 16:45 453 查看
首先导入AddressBook.framework
#import <AddressBook/AddressBook.h>

下面是代码
-(NSMutableArray *) getAllContacts{
NSMutableArray *contactsArray = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *personArray = [[[NSMutableArray alloc] init] autorelease];

ABAddressBookRef addressBook = ABAddressBookCreate();
NSString *firstName, *lastName, *fullName;
personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

NSMutableArray *contact;
for (id *person in personArray){
contact = [[NSMutableArray alloc] init];
firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
firstName = [firstName stringByAppendingFormat:@" "];
lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
fullName = [firstName stringByAppendingFormat:@"%@",lastName];
NSLog(@"fullName = %@",fullName);

ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
for(int i = 0 ;i < ABMultiValueGetCount(phones); i++){
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
[contact addObject:phone];
}

ABMultiValueRef mails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
for(int i = 0 ;i < ABMultiValueGetCount(mails); i++){
NSString *mail = (NSString *)ABMultiValueCopyValueAtIndex(mails, i);
[contact addObject:mail];
}
[contactsArray addObject:contact];// add contact into array
[contact release];
}
return contactsArray;
}


文章转自:http://gaohaijun.blog.163.com/blog/static/1766982712010112294748607/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: