新建一个CContact类用于存放联系人信息,下面是该类的代码:
CContact.h代码:
01 |
#import <Foundation/Foundation.h> |
04 |
@interface CContact : NSObject |
06 |
@property (nonatomic,strong) NSString * firstName; |
07 |
@property (nonatomic,strong) NSString * lastName; |
08 |
@property (nonatomic,strong) NSString * compositeName; |
09 |
@property (nonatomic,strong) UIImage * image; |
10 |
@property (nonatomic,strong) NSMutableDictionary * phoneInfo; |
11 |
@property (nonatomic,strong) NSMutableDictionary * emailInfo; |
12 |
@property (nonatomic,assign) int recordID;
|
13 |
@property (nonatomic,assign) int sectionNumber;
|
16 |
//-(NSString *)getFirstName; |
17 |
//-(NSString *)getLastName; |
18 |
-(NSString *)getFisrtLetterForCompositeName; |
CContact.m文件代码:
05 |
// Created by Carl on 13-6-28. |
06 |
// Copyright (c) 2013年 contact..com. All rights reserved. |
12 |
@implementation CContact |
18 |
[_compositeName release];
|
36 |
-(NSMutableDictionary *)phoneInfo |
40 |
_phoneInfo = [[NSMutableDictionary alloc] init];
|
47 |
-(NSMutableDictionary *)emailInfo |
51 |
_emailInfo = [[NSMutableDictionary alloc] init];
|
下面是一个自定义函数,用于取得所有联系人信息。
002 |
-(NSMutableArray *)allContacts |
008 |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
010 |
NSArray * contacts = (NSArray *)ABAddressBookCopyArrayOfAllPeople(self.addressBook);
|
013 |
// if([_allContacts retainCount] > 0) [_allContacts release]; |
015 |
_allContacts = [[NSMutableArray alloc] init];
|
018 |
int contactsCount = [contacts count];
|
020 |
for ( int i = 0; i < contactsCount; i++)
|
022 |
ABRecordRef record = (ABRecordRef)[contacts objectAtIndex:i];
|
023 |
CContact * contact = [[CContact alloc] init];
|
025 |
CFStringRef firstNameRef = ABRecordCopyValue(record, kABPersonFirstNameProperty);
|
026 |
contact.firstName = (NSString *)firstNameRef;
|
027 |
CFStringRef lastNameRef = ABRecordCopyValue(record, kABPersonLastNameProperty);
|
028 |
contact.lastName = (NSString *)lastNameRef;
|
029 |
CFStringRef compositeNameRef = ABRecordCopyCompositeName(record);
|
030 |
contact.compositeName = (NSString *)compositeNameRef;
|
032 |
firstNameRef != NULL ? CFRelease(firstNameRef) : NULL;
|
033 |
lastNameRef != NULL ? CFRelease(lastNameRef) : NULL;
|
034 |
compositeNameRef != NULL ? CFRelease(compositeNameRef) : NULL;
|
037 |
contact.recordID = ( int )ABRecordGetRecordID(record);
|
042 |
if (ABPersonHasImageData(record))
|
044 |
// NSData * imageData = ( NSData *)ABPersonCopyImageData(record); |
045 |
NSData * imageData = (NSData *)ABPersonCopyImageDataWithFormat(record,kABPersonImageFormatThumbnail);
|
046 |
UIImage * image = [[UIImage alloc] initWithData:imageData];
|
050 |
contact.image = image;
|
058 |
ABMultiValueRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
|
059 |
for ( int i = 0; i < ABMultiValueGetCount(phones); i++)
|
062 |
CFStringRef phoneLabelRef = ABMultiValueCopyLabelAtIndex(phones, i);
|
063 |
CFStringRef localizedPhoneLabelRef = ABAddressBookCopyLocalizedLabel(phoneLabelRef);
|
064 |
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, i);
|
066 |
NSString * localizedPhoneLabel = (NSString *) localizedPhoneLabelRef;
|
067 |
NSString * phoneNumber = (NSString *)phoneNumberRef;
|
069 |
[contact.phoneInfo setValue:phoneNumber forKey:localizedPhoneLabel];
|
072 |
phoneLabelRef == NULL ? : CFRelease(phoneLabelRef);
|
073 |
localizedPhoneLabelRef == NULL ? : CFRelease(localizedPhoneLabelRef);
|
074 |
phoneNumberRef == NULL ? : CFRelease(phoneNumberRef);
|
077 |
if (phones != NULL) CFRelease(phones);
|
080 |
ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
|
081 |
for ( int i = 0; i < ABMultiValueGetCount(emails); i++)
|
084 |
CFStringRef emailLabelRef = ABMultiValueCopyLabelAtIndex(emails, i);
|
085 |
CFStringRef localizedEmailLabelRef = ABAddressBookCopyLocalizedLabel(emailLabelRef);
|
086 |
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, i);
|
088 |
NSString * localizedEmailLabel = ( NSString *)localizedEmailLabelRef;
|
090 |
NSString * email = (NSString *)emailRef;
|
092 |
[contact.emailInfo setValue:email forKey:localizedEmailLabel];
|
094 |
emailLabelRef == NULL ? : CFRelease(emailLabelRef);
|
095 |
localizedEmailLabel == NULL ? : CFRelease(localizedEmailLabelRef);
|
096 |
emailRef == NULL ? : CFRelease(emailRef);
|
099 |
if (emails != NULL) CFRelease(emails);
|
102 |
[_allContacts addObject:contact];
|
109 |
return [_allContacts autorelease];
|