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

首先导入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/
阅读更多
个人分类: iPhone
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭