0

我是 iPhone 新手。我被困在我的项目中,主要是将所有地址簿联系人(姓名和电子邮件)放在我的项目中创建的表格视图中,这些联系人主要放在 iphone 设备中。我该怎么做?

4

3 回答 3

3
NSMutableArray* contactsArray = [NSMutableArray new];

// open the default address book. 
ABAddressBookRef m_addressbook = ABAddressBookCreate();

if (!m_addressbook) 
{
    NSLog(@"opening address book");
}
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++) 
{ 
    NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
    [tempContactDic setValue:name forKey:@"name"];
    //fetch email id
    NSString *strEmail;
    ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
    CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
    strEmail = (__bridge  NSString *)tempEmailref;

    [tempContactDic setValue:strEmail forKey:@"email"];

     [contactsArray addObject:tempContactDic];

}

所有联系人数据保存在联系人数组中。然后您可以根据需要使用此数组。

于 2012-05-09T06:58:22.047 回答
0

使用地址簿 api 来执行此操作。在这里查看我的答案

不使用联系人选择器的 iPhone 上的联系人列表

有关更多详细信息,请在此处获取苹果文档。

于 2012-05-09T06:27:09.377 回答
0
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

-(void)viewWillAppear:(BOOL)animated{

    NSMutableArray* contactsArray = [NSMutableArray new];

    ABAddressBookRef m_addressbook = ABAddressBookCreate();

    if (!m_addressbook)
    {
        NSLog(@"opening address book");
    }
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
    for (int i=0;i < nPeople;i++)
    {
        NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
        NSLog(@"tempContactDic ios a ==%@",tempContactDic);
       CFStringRef firstName, lastName;
        firstName = (__bridge CFStringRef)((__bridge UILabel *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty)));
        lastName  = (__bridge CFStringRef)((__bridge UILabel *)(ABRecordCopyValue(ref, kABPersonLastNameProperty)));
      //  [tempContactDic setValue:name forKey:@"name"];
        //fetch email id
        NSString *strEmail;
        ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
        CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
        strEmail = (__bridge  NSString *)tempEmailref;

        [tempContactDic setValue:strEmail forKey:@"email"];

        [contactsArray addObject:tempContactDic];

    }

}
-(IBAction)getcontactlist:(id)sender{

         [self getContact];
}

-(IBAction)getContact {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    firstName.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    lastName.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
    number.text = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
    return YES;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    return NO;
}

这段代码对我来说很好......

于 2014-01-24T11:06:04.477 回答