iOS取得AddressBook联系人信息

新建一个CContact类用于存放联系人信息,下面是该类的代码:

CContact.h代码:

01 #import <Foundation/Foundation.h>
02  
03  
04 @interface CContact : NSObject
05  
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;
14  
15  
16 //-(NSString *)getFirstName;
17 //-(NSString *)getLastName;
18 -(NSString *)getFisrtLetterForCompositeName;
19  
20 @end

CContact.m文件代码:

01 //
02 //  CContact.m
03 //  Contact
04 //
05 //  Created by Carl on 13-6-28.
06 //  Copyright (c) 2013年 contact..com. All rights reserved.
07 //
08  
09 #import "CContact.h"
10 #import "pinyin.h"
11  
12 @implementation CContact
13  
14 -(void)dealloc
15 {
16     [_firstName release];
17     [_lastName release];
18     [_compositeName release];
19     [_image release];
20     [_phoneInfo release];
21     [_emailInfo release];
22      
23  
24     _recordID = nil;
25     _firstName = nil;
26     _lastName = nil;
27     _compositeName = nil;
28     _image = nil;
29     _phoneInfo = nil;
30     _emailInfo = nil;
31      
32     [super dealloc];
33 }
34  
35  
36 -(NSMutableDictionary *)phoneInfo
37 {
38     if(_phoneInfo == nil)
39     {
40         _phoneInfo = [[NSMutableDictionary alloc] init];
41     }
42      
43     return _phoneInfo;
44 }
45  
46  
47 -(NSMutableDictionary *)emailInfo
48 {
49     if(_emailInfo == nil)
50     {
51         _emailInfo = [[NSMutableDictionary alloc] init];
52     }
53     return  _emailInfo;
54 }
55  
56  
57  
58 @end

下面是一个自定义函数,用于取得所有联系人信息。

001 //取得所有的联系人
002 -(NSMutableArray *)allContacts
003 {
004      
005      
006      
007      
008     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
009  
010     NSArray * contacts = (NSArray *)ABAddressBookCopyArrayOfAllPeople(self.addressBook);
011      
012      
013 //    if([_allContacts retainCount] > 0) [_allContacts release];
014      
015     _allContacts = [[NSMutableArray alloc] init];
016      
017      
018     int contactsCount = [contacts count];
019      
020     for(int i = 0; i < contactsCount; i++)
021     {
022         ABRecordRef record = (ABRecordRef)[contacts objectAtIndex:i];
023         CContact * contact = [[CContact alloc] init];
024         //取得姓名
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;
031          
032         firstNameRef != NULL ? CFRelease(firstNameRef) : NULL;
033         lastNameRef != NULL ? CFRelease(lastNameRef) : NULL;
034         compositeNameRef != NULL ? CFRelease(compositeNameRef) : NULL;
035  
036         //取得联系人的ID
037         contact.recordID = (int)ABRecordGetRecordID(record);
038         
039          
040          
041         //联系人头像
042         if(ABPersonHasImageData(record))
043         {
044 //            NSData * imageData = ( NSData *)ABPersonCopyImageData(record);
045             NSData * imageData = (NSData *)ABPersonCopyImageDataWithFormat(record,kABPersonImageFormatThumbnail);
046             UIImage * image = [[UIImage alloc] initWithData:imageData];
047              
048             [imageData release];
049              
050             contact.image = image;
051              
052             [image release];
053         
054         }
055          
056          
057         //处理联系人电话号码
058         ABMultiValueRef  phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
059         for(int i = 0; i < ABMultiValueGetCount(phones); i++)
060         {
061              
062             CFStringRef phoneLabelRef = ABMultiValueCopyLabelAtIndex(phones, i);
063             CFStringRef localizedPhoneLabelRef = ABAddressBookCopyLocalizedLabel(phoneLabelRef);
064             CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, i);
065  
066             NSString * localizedPhoneLabel = (NSString *) localizedPhoneLabelRef;
067             NSString * phoneNumber = (NSString *)phoneNumberRef;
068              
069             [contact.phoneInfo setValue:phoneNumber forKey:localizedPhoneLabel];
070              
071             //释放内存
072             phoneLabelRef == NULL ? : CFRelease(phoneLabelRef);
073             localizedPhoneLabelRef == NULL ? : CFRelease(localizedPhoneLabelRef);
074             phoneNumberRef == NULL ? : CFRelease(phoneNumberRef);
075              
076         }
077         if(phones != NULL) CFRelease(phones);
078          
079         //处理联系人邮箱
080         ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
081         for(int i = 0; i < ABMultiValueGetCount(emails); i++)
082         {
083              
084             CFStringRef emailLabelRef = ABMultiValueCopyLabelAtIndex(emails, i);
085             CFStringRef localizedEmailLabelRef = ABAddressBookCopyLocalizedLabel(emailLabelRef);
086             CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, i);
087              
088             NSString * localizedEmailLabel = ( NSString *)localizedEmailLabelRef;
089              
090             NSString * email = (NSString *)emailRef;
091              
092             [contact.emailInfo setValue:email forKey:localizedEmailLabel];
093              
094             emailLabelRef == NULL ? : CFRelease(emailLabelRef);
095             localizedEmailLabel == NULL ? : CFRelease(localizedEmailLabelRef);
096             emailRef == NULL ? : CFRelease(emailRef);
097              
098         }
099         if(emails != NULL) CFRelease(emails);
100          
101          
102         [_allContacts addObject:contact];
103         [contact release];
104         CFRelease(record);
105          
106     }
107  
108     [pool drain];   
109     return [_allContacts autorelease];
110      
111 }
上一篇:springboot pom 引用集合


下一篇:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)