2012-05-09 15 views
0

Я новичок в iphone. Я застрял в своем проекте в процессе получения всех контактов адресной книги в основном (имя и адрес электронной почты), которые помещаются в устройство iphone, в мое представление таблицы, которое создано в моем проекте. Как мне это сделать?Как получить все контакты адресной книги с iphone

+0

дубликатом вопрос на http://stackoverflow.com/questions/1558543/iphone-address-book-sample-code официальный документ здесь HTTPS : //developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/ https://developer.apple.com/iPhone/library/documentation/AddressBookUI/Reference/AddressBookUI_Functions/index.html – adali

ответ

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]; 

} 

все контакты Данные, сохраненные в Контакты Массив. Затем вы можете использовать этот массив в соответствии с вашими потребностями.

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; 
} 

Этот код работает отлично для меня ...

Смежные вопросы