获取 iPhone 上联系人姓名、电话、邮件的代码

  
    
iOS4 已经开放了查询通讯录信息的功能,下面分享一段会员cctv的获取
iPhone 上存储的联系人姓名、电话、邮件的代码

苹果 iPhone 是允许查询讯录信息的,这就为很多第三方通讯类应用提供了方便。下面分享一段会员“cctv”的获取 iPhone 上存储的联系人姓名、电话、邮件的代码

+ (NSMutableArray *) getAllContacts

{

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

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

    

    ABAddressBookRef addressBook = ABAddressBookCreate();

    NSString *firstName, *lastName, *fullName;

    personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    

    Contacts *contact;

    for (id *person in personArray)

    {

        contact = [[Contacts alloc] init];    

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

        firstName = [firstName stringByAppendingFormat:@" "];

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

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

        contact.contactName = fullName;

        

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

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

        {  

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

            [contact.contactPhoneArray addObject:phone];

        }

        

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

        for(int i = 0 ;i < ABMultiValueGetCount(mails); i++)

        {  

            NSString *mail = (NSString *)ABMultiValueCopyValueAtIndex(mails, i);

            [contact.contactMailArray addObject:mail];

        }        

        [contactsArray addObject:contact];   // add contact into array

        [contact release];

    }    

    return contactsArray;

}

你可能感兴趣的:(iPhone)