获取 iPhone 上联系人姓名、电话、邮件的代码
苹果 iPhone 是允许查询讯录信息的,这就为很多第三方通讯类应用提供了方便。
下面分享一段会员“cctv”的获取 iPhone 上存储的联系人姓名、电话、邮件的代码.
1 + (NSMutableArray *) getAllContacts
2 {
3 NSMutableArray *contactsArray = [[[NSMutableArray alloc] init] autorelease];
4 NSMutableArray* personArray = [[[NSMutableArray alloc] init] autorelease];
5
6 ABAddressBookRef addressBook = ABAddressBookCreate();
7 NSString *firstName, *lastName, *fullName;
8 personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
9
10 Contacts *contact;
11 for (id *person in personArray)
12 {
13 contact = [[Contacts alloc] init];
14 firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
15 firstName = [firstName stringByAppendingFormat:@" "];
16 lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
17 fullName = [firstName stringByAppendingFormat:@"%@",lastName];
18 contact.contactName = fullName;
19
20 ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
21 for(int i = 0 ;i < ABMultiValueGetCount(phones); i++)
22 {
23 NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
24 [contact.contactPhoneArray addObject:phone];
25 }
26
27 ABMultiValueRef mails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
28 for(int i = 0 ;i < ABMultiValueGetCount(mails); i++)
29 {
30 NSString *mail = (NSString *)ABMultiValueCopyValueAtIndex(mails, i);
31 [contact.contactMailArray addObject:mail];
32 }
33 [contactsArray addObject:contact]; // add contact into array
34 [contact release];
35 }
36 return contactsArray;
37 }
-------------
胖叔——zhulin1987.com
胖叔——zhulin1987.com