2013-01-16 24 views
1

我想在我的应用程序中显示联系人和联系人的详细信息。联系人列表以及在选择联系人的任何联系人详细信息后,将使用地址簿显示在下一页上。我正在使用IOS 6. 在此先感谢。如何在IOS中使用iPhone的联系人6

回答

7

检索下面的代码联系方式。

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    contactList=[[NSMutableArray alloc] init]; 
    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 *dOfPerson=[NSMutableDictionary dictionary]; 

     ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 

     //For username and surname 
     ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty); 
     CFStringRef firstName, lastName; 
     firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); 
     lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); 
     [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"]; 

     //For Email ids 
     ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty); 
     if(ABMultiValueGetCount(eMail) > 0) { 
      [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"]; 

     } 

     //For Phone number 
     NSString* mobileLabel; 
     for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) { 
      mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i); 
      if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) 
      { 
       [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"]; 
      } 
      else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) 
      { 
       [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"]; 
       break ; 
      } 

     [contactList addObject:dOfPerson]; 
     CFRelease(ref); 
     CFRelease(firstName); 
     CFRelease(lastName); 
    } 
    NSLog(@"array is %@",contactList); 
    } 
} 
+1

欲了解更多详情和示例代码,请点击这里.http://goo.gl/V4EYa –

0

查看Apple的AddressBook编程指南。这会给你你需要有关使用地址簿工作的所有信息 -

ABRecordID recordId; 
ABAddressBookRef _addressBookRef = ABAddressBookCreate(); 
NSArray* allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef); 
NSString* compositeName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record); 
recordId = ABRecordGetRecordID(record); 
ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty); 

做适当的释放只是一个片段

http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Introduction.html#//apple_ref/doc/uid/TP40007744

+0

使用此答案。由于这是来自文档,它会真正帮助您了解有关地址簿的其他许多事情。 – Hemang

2

添加Framwork

#import <AddressBook/AddressBook.h> 
#import <AddressBook/ABPerson.h> 
#import <AddressBookUI/AddressBookUI.h> 

使用委托

<ABPeoplePickerNavigationControllerDelegate,ABPersonViewControllerDelegate,ABNewPersonViewControllerDelegate,ABUnknownPersonViewControllerDelegate> 

可以显示通过ABPeoplePickerNavigationController

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; 
     [[picker navigationBar] setBarStyle:UIBarStyleBlack]; 
     picker.peoplePickerDelegate = self; 
     // Display only a person's phone, email, and birthdate 
     NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil]; 

     picker.displayedProperties = displayedItems; 
     // Show the picker 
     [self presentModalViewController:picker animated:YES]; 
     [picker release]; 

您联系并为您初始化,然后后,您需要使用下面的方法

#pragma mark - ABPeopelPickerNavigationController Delegate and DataSource Methods 

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

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{ 
    // For get People detail 
} 

- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person 
{ 
} 

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person 
{ 
} 

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier 
{ 
    return YES; 
} 

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier; 
{ 
    return YES; 
} 
相关问题