iPhone查询通讯录中的联系人信息(电话号码及邮箱)

1.首先要在Frameworks中添加“AddressBook.framework”
2.自定义联系人类

#import <Foundation/Foundation.h>

@interface ZQPerson : NSObject {

    NSString *firstName;

    NSString *lastName;

    NSString *fullName;

    NSString *phone;

    NSString *email;

}


@property (nonatomicretainNSString *firstName;

@property (nonatomicretainNSString *lastName;

@property (nonatomicretainNSString *fullName;

@property (nonatomicretainNSString *phone;

@property (nonatomicretainNSString *email;


@end


 

#import "ZQPerson.h"


@implementation ZQPerson


@synthesize firstName;

@synthesize lastName;

@synthesize fullName;

@synthesize phone;

@synthesize email;


- (void) dealloc {

    

       [firstName release];

    [lastName release];

    [fullName release];

    [phone release];

    [email release];

    [super dealloc];


}


@end


3.通讯录工具类

#import <Foundation/Foundation.h>


#import "ZQPerson.h"


 

@interface ZQAddressBookTool : NSObject {

    

}


 

+ (NSMutableArray *) getAddressBookList;


@end


 

#import "ZQAddressBookTool.h"

#import "TaGeDemoViewController.h"


@implementation ZQAddressBookTool


 

+ (NSMutableArray *) getAddressBookList {

    

    NSMutableArray *personArray = [[[NSMutableArray allocinitautorelease];

    

       NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];

//  NSMutableArray *array = [[NSMutableArray alloc] init];

    

    ABAddressBookRef addressBook = ABAddressBookCreate();

    

    NSString *firstName;

    

    NSString *lastName;

    

    NSString *fullName;

    

    personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    

    for (id *person in personArray) {

        

        

        ZQPerson *p = [[ZQPerson allocinit];

        

        firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

        

        firstName = [firstName stringByAppendingFormat:@""];

        

        lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

        

        fullName = [firstName stringByAppendingFormat:@"%@",lastName];

        

        p.firstName = firstName;

        

        p.lastName = lastName;

        

        p.fullName = fullName;

        

        ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person,kABPersonPhoneProperty);

        

        for (int i = 0; i < ABMultiValueGetCount(phones); i ++) {

            

            NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);

            

            p.phone = phone;

            

            [phone release];

            

        }

        

        ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person,kABPersonEmailProperty);

        

        for (int i = 0; i < ABMultiValueGetCount(emails); i ++) {

            

            NSString *email = (NSString *)ABMultiValueCopyValueAtIndex(emails, i);

            

            p.email = email;

            

            [email release];

            

        }

        

        [firstName release];

        

        [lastName release];

        

        [fullName release];

        

        

        [array addObject:p];

        

        [p release];

    }

    

    return array;

} // getAddressBookList


@end

4.调用方法

    NSMutableArray *persons = [ZQAddressBookTool getAddressBookList];


    for (ZQPerson *person in persons) {

        

        NSLog(person.fullName);

        

        NSLog(person.phone);

        

        NSLog(person.email);

        

        [persons release];

    }

    

    [persons release];

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值