Created
January 2, 2013 20:00
-
-
Save codeswimmer/4437445 to your computer and use it in GitHub Desktop.
iOS: How To Get The Record Of Your Contacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Add first all required framework to gain access of your device Address Book. | |
| AddressBook.framework and AddressBookUI.framework | |
| Then import these framworks to your controller. | |
| #import <AddressBook/AddressBook.h> | |
| #import <AddressBookUI/AddressBookUI.h> | |
| There are two ways to get your contacts. First is use the ABPeoplePickerNavigationController, the other is to use the ABAddressBookRef. | |
| 1. Contacts Picker Controller (ABPeoplePickerNavigationController): | |
| Add to your @interface this delegate <ABPeoplePickerNavigationControllerDelegate> then go and implement the code below to have a contacts picker controller. | |
| ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init]; | |
| picker.peoplePickerDelegate = self; | |
| [self presentModalViewController:picker animated:YES]; | |
| [picker release]; | |
| Now, the use of that Delegate we added to our @interface is when users press cancel, and add events when selecting any of your contacts. Sample of Delegate method to implement is the one below to dismiss the popup contacts view. | |
| - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { | |
| [peoplePicker dismissModalViewControllerAnimated:YES]; | |
| } | |
| 2. Get a list of contacts (ABAddressBookRef): | |
| Implement this method first to your controller (Returns an array of contacts). | |
| Note that this is not a standard objective-c method definition so it should be defined and implemented outside the @interface and @implementation. | |
| NSMutableArray* getArrayOfContactsFromAddressBook(ABAddressBookRef m_addressbook) { | |
| // init array | |
| NSMutableArray *mArray = [[[NSMutableArray alloc] init] autorelease]; | |
| // can be cast to NSArray, toll-free | |
| CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook); | |
| CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook); | |
| // CFStrings can be cast to NSString! | |
| for (int i=0;i < nPeople;i++) { | |
| // Record Reference | |
| ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); | |
| CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty); | |
| CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty); | |
| NSString *contactFirstLast = [NSString stringWithFormat:@"%@ %@", firstName, lastName]; | |
| CFRelease(firstName); | |
| CFRelease(lastName); | |
| CFRelease(ref); | |
| [mArray addObject:contactFirstLast]; | |
| NSLog(@"Contact: %@", contactFirstLast); | |
| } | |
| CFRelease(allPeople); | |
| return mArray; | |
| } | |
| To use that method above, we need to have a reference of our Address Book first and pass that reference to the parameter of the method we defined and implemented above. | |
| ABAddressBookRef m_addressbook = ABAddressBookCreate(); | |
| // check if ur address book ref is initialized | |
| if (!m_addressbook) { | |
| NSLog(@"opening address book"); | |
| } | |
| self.contactsArray = [[[NSMutableArray alloc] initWithArray:getArrayOfContactsFromAddressBook(m_addressbook)] autorelease]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment