iOS取得AddressBook联系人信息

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

    CContact.h代码:

#import <Foundation/Foundation.h>


@interface CContact : NSObject

@property (nonatomic,strong) NSString * firstName;
@property (nonatomic,strong) NSString * lastName;
@property (nonatomic,strong) NSString * compositeName;
@property (nonatomic,strong) UIImage * image;
@property (nonatomic,strong) NSMutableDictionary * phoneInfo;
@property (nonatomic,strong) NSMutableDictionary * emailInfo;
@property (nonatomic,assign) int recordID;
@property (nonatomic,assign) int sectionNumber;


//-(NSString *)getFirstName;
//-(NSString *)getLastName;
-(NSString *)getFisrtLetterForCompositeName;

@end
   CContact.m文件代码:

//
//  CContact.m
//  Contact
//
//  Created by Carl on 13-6-28.
//  Copyright (c) 2013年 contact..com. All rights reserved.
//

#import "CContact.h"
#import "pinyin.h"

@implementation CContact

-(void)dealloc
{
    [_firstName release];
    [_lastName release];
    [_compositeName release];
    [_image release];
    [_phoneInfo release];
    [_emailInfo release];
    

    _recordID = nil;
    _firstName = nil;
    _lastName = nil;
    _compositeName = nil;
    _image = nil;
    _phoneInfo = nil;
    _emailInfo = nil;
    
    [super dealloc];
}


-(NSMutableDictionary *)phoneInfo
{
    if(_phoneInfo == nil)
    {
        _phoneInfo = [[NSMutableDictionary alloc] init];
    }
    
    return _phoneInfo;
}


-(NSMutableDictionary *)emailInfo
{
    if(_emailInfo == nil)
    {
        _emailInfo = [[NSMutableDictionary alloc] init];
    }
    return  _emailInfo;
}



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

//取得所有的联系人
-(NSMutableArray *)allContacts
{
    
    
    
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSArray * contacts = (NSArray *)ABAddressBookCopyArrayOfAllPeople(self.addressBook);
    
    
//    if([_allContacts retainCount] > 0) [_allContacts release];
    
    _allContacts = [[NSMutableArray alloc] init];
    
    
    int contactsCount = [contacts count];
    
    for(int i = 0; i < contactsCount; i++)
    {
        ABRecordRef record = (ABRecordRef)[contacts objectAtIndex:i];
        CContact * contact = [[CContact alloc] init];
        //取得姓名
        CFStringRef  firstNameRef =  ABRecordCopyValue(record, kABPersonFirstNameProperty);
        contact.firstName = (NSString *)firstNameRef;
        CFStringRef lastNameRef = ABRecordCopyValue(record, kABPersonLastNameProperty);
        contact.lastName = (NSString *)lastNameRef;
        CFStringRef compositeNameRef = ABRecordCopyCompositeName(record);
        contact.compositeName = (NSString *)compositeNameRef;
        
        firstNameRef != NULL ? CFRelease(firstNameRef) : NULL;
        lastNameRef != NULL ? CFRelease(lastNameRef) : NULL;
        compositeNameRef != NULL ? CFRelease(compositeNameRef) : NULL;

        //取得联系人的ID
        contact.recordID = (int)ABRecordGetRecordID(record);
       
        
        
        //联系人头像
        if(ABPersonHasImageData(record))
        {
//            NSData * imageData = ( NSData *)ABPersonCopyImageData(record);
            NSData * imageData = (NSData *)ABPersonCopyImageDataWithFormat(record,kABPersonImageFormatThumbnail);
            UIImage * image = [[UIImage alloc] initWithData:imageData];
            
            [imageData release];
            
            contact.image = image;
            
            [image release];
       
        }
        
        
        //处理联系人电话号码
        ABMultiValueRef  phones = ABRecordCopyValue(record, kABPersonPhoneProperty);
        for(int i = 0; i < ABMultiValueGetCount(phones); i++)
        {
            
            CFStringRef phoneLabelRef = ABMultiValueCopyLabelAtIndex(phones, i);
            CFStringRef localizedPhoneLabelRef = ABAddressBookCopyLocalizedLabel(phoneLabelRef);
            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, i);

            NSString * localizedPhoneLabel = (NSString *) localizedPhoneLabelRef;
            NSString * phoneNumber = (NSString *)phoneNumberRef;
            
            [contact.phoneInfo setValue:phoneNumber forKey:localizedPhoneLabel];
            
            //释放内存
            phoneLabelRef == NULL ? : CFRelease(phoneLabelRef);
            localizedPhoneLabelRef == NULL ? : CFRelease(localizedPhoneLabelRef);
            phoneNumberRef == NULL ? : CFRelease(phoneNumberRef);
            
        }
        if(phones != NULL) CFRelease(phones);
        
        //处理联系人邮箱
        ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
        for(int i = 0; i < ABMultiValueGetCount(emails); i++)
        {
            
            CFStringRef emailLabelRef = ABMultiValueCopyLabelAtIndex(emails, i);
            CFStringRef localizedEmailLabelRef = ABAddressBookCopyLocalizedLabel(emailLabelRef);
            CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, i);
            
            NSString * localizedEmailLabel = ( NSString *)localizedEmailLabelRef;
            
            NSString * email = (NSString *)emailRef;
            
            [contact.emailInfo setValue:email forKey:localizedEmailLabel];
            
            emailLabelRef == NULL ? : CFRelease(emailLabelRef);
            localizedEmailLabel == NULL ? : CFRelease(localizedEmailLabelRef);
            emailRef == NULL ? : CFRelease(emailRef);
            
        }
        if(emails != NULL) CFRelease(emails);
        
        
        [_allContacts addObject:contact];
        [contact release];
        CFRelease(record);
        
    }

    [pool drain];    
    return [_allContacts autorelease];
    
}

转载于:https://my.oschina.net/CarlHuang/blog/146286

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值