ABPeoplePickerNavigationController display all contact data in a single cel - iphone

Hi i am developing a app where when i click contacts it goes to ABpeoplePicekrNavigationcontroller and display all contacts in the form of list in the table.But i want to select all contacts so that i want to perform some action.So how can i achive this.
I am not sure this is the right question to ask.Actually i am trying to send the contact information to next screen instead of selecting single contact information everytime i want to select whole contact list.So how can i do this??
I thought i show them to show in single cell even though space doesnt fit in single cell.just show them single cell so that selecting one cell which contains all contact information would be better to send..
So can anyone suggest me right way to select all the contacts list rather than selecting one contact..I donno whether we can do this or not??if so how???if not what is the other way??
Here is the below code where i am using for accessing the contact list.
- (IBAction)configureMyContact:(id)sender {
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
peoplePicker.peoplePickerDelegate = self;
peoplePicker.navigationBar.topItem.title = NSLocalizedString(#"CHOOSE_CONTACT_TITLE", #"Defining my contact title.");
[self presentModalViewController:peoplePicker animated:YES];
[peoplePicker release];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
myContactID = ABRecordGetRecordID(person);
[self refreshMyContactButton];
[self saveMyContactID:myContactID];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier{
return NO;
}

You can get your record using this
- (void)getPersonOutOfAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil)
{
NSLog(#"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i < [allContacts count]; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (int k=0;k<ABMultiValueGetCount(mobile); k++)
{
NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
}
//email
ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
NSUInteger j = 0;
for (j = 0; j < ABMultiValueGetCount(emails); j++)
{
NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
}
}
}
CFRelease(addressBook);
}

Related

Use of undeclared identifier PhonesViewController ios7 xCode

Can you please help me with this error that I'am getting...
Basically, I'm trying to import all my contact list from my address book. Also I'm checking the phone number label, but the error that I'm getting is "Use of undeclared identifier PhonesViewController ios7 xCode" and I have that viewController, and also I have added the class in storyboard for the viewcontroller although I think that it is not necessary
Thanks in advance
Here is my code:
#import "PhonesViewController.h"
#implementation PhonesViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
- (BOOL)PhohesViewController: (ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
// Name of contact.
NSString* name = (NSString *)ABRecordCopyCompositeName(person);
// Numbers of selected contact
ABMutableMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableString *mobile = [[NSMutableString alloc] init];
NSMutableString *office = [[NSMutableString alloc] init];
// Getting if Mobile, Office(work) numbers exist
for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
{
// Number in contact details of current index
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, numberIndex);
// Label of Phone Number
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, numberIndex);
NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
// Phone number
NSString *phoneNumber = (NSString *)phoneNumberRef;
// Release Phone Number and locationLabel reference object
CFRelease(phoneNumberRef);
CFRelease(locLabel);
NSLog(#" - %# (%#)", phoneNumber, phoneLabel);
if ([phoneLabel isEqualToString:#"mobile"])// Mobile number saving.
{
[mobile appendFormat:#"%#", phoneNumber];
}
else if ([phoneLabel isEqualToString:#"work"])// Office number saving.
{
[office appendFormat:#"%#", phoneNumber];
}
[phoneNumber release];
}
CFRelease(phones);
// Emails of selected contact
ABMutableMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableString *generalMail = [[NSMutableString alloc] init];
NSMutableString *officeMail = [[NSMutableString alloc] init];
// Getting if Home, Office(work) mails exist
for(CFIndex numberIndex = 0; numberIndex < ABMultiValueGetCount(emails); numberIndex++)
{
// Mail in contact details of current index
CFStringRef mailRef = ABMultiValueCopyValueAtIndex(emails, numberIndex);
// Label of Phone Number
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(emails, numberIndex);
NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
// Phone number
NSString *mail = (NSString *)mailRef;
// Release Phone Number and locationLabel reference object
CFRelease(mailRef);
CFRelease(locLabel);
NSLog(#" - %# (%#)", mail, mailLabel);
if ([mailLabel isEqualToString:#"mobile"])// Home mail.
{
[generalMail appendFormat:#"%#", mail];
}
else if ([mailLabel isEqualToString:#"work"])// Office(Work) mail.
{
[officeMail appendFormat:#"%#", mail];
}
[mail release];
}
CFRelease(emails);
[mobile release];
[office release];
[generalMail release];
[officeMail release];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
}
#end
Looks like a typo to me.
// Phohes?
- (BOOL)PhohesViewController: (ABPeoplePickerNavigationController *)picker //Phohes? shouldContinueAfterSelectingPerson:(ABRecordRef)person
Looking at your code and the image you've posted, you're defining a method within a method, you can't do that.

Problem when retrieving address, photo from iphone address book

I have a task to develop the application with phone gap. And retrieve in my application.using javascript.
I want first name,last name,address,photo,home page,email,phone no in my application using phone gap. i have done the following code to retrive them. but i am not getting photo and address in my field.
I have done all code to retrieve the contact in native code. and i am sending it to the javascript file by creating the jsstring object of javascript function.
This is my native implement to read address book data.
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)picker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
/** Fetch Contact name */
name = (NSString *)ABRecordCopyCompositeName(person);
NSLog(#"name:%#",name);
/** Fetch Mobile number */
ABMultiValueRef *phones = (ABMultiValueRef *) ABRecordCopyValue(person, kABPersonPhoneProperty);
mobile=[[NSString alloc] initWithString:#""];
NSString *mobileLabel = [[NSString alloc] initWithString:#""];
for (CFIndex i=0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString *) ABMultiValueCopyLabelAtIndex(phones, i);
if ([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
mobile = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
}
else if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) {
mobile = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
}
}
/** Fetch Email addres */
emailID = [NSString stringWithFormat:#""];
ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i < ABMultiValueGetCount(emails); i++) {
[emailID release];
emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
NSLog(#"emailid:%#",emailID);
}
/** Fetch Homepage */
ABMultiValueRef homepage = (ABMultiValueRef *) ABRecordCopyValue(person, kABPersonURLProperty);
NSString *homepageLabel = [NSString stringWithFormat:#""];
homepageID = [NSString stringWithFormat:#""];
for(CFIndex i=0; i < ABMultiValueGetCount(homepage);i++)
{
homepageLabel = (NSString *) ABMultiValueCopyLabelAtIndex(homepage, i);
if([homepageLabel isEqualToString:(NSString *)kABPersonHomePageLabel]){
homepageID = (NSString *)ABMultiValueCopyValueAtIndex(homepage, i);
NSLog(#"homepage:%#",homepageID);
}
}
/** Fatch Image */
NSData *imgData = (NSData *)ABPersonCopyImageData(person);
// call the js to fill the all contact infromation
{
NSString* jsString = [[NSString alloc] initWithFormat:#"fillContactInfo('%#','%#','%#','%#','%#');",name ,mobile ,emailID, homepageID, imgData];
NSLog(#"jstring:%#",jsString);
[webView stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
}
[emailID release];
[mobile release];
[mobileLabel release];
[homepageID release];
[homepageLabel release];
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier{
/*
* Set up an ABMultiValue to hold the address values; copy from address
* book record.
*/
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
// Set up an NSArray and copy the values in.
NSArray *theArray = [(id)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
// Figure out which values we want and store the index.
const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi, identifier);
// Set up an NSDictionary to hold the contents of the array.
NSDictionary *theDict = [theArray objectAtIndex:theIndex];
// Set up NSStrings to hold keys and values. First, how many are there?
const NSUInteger theCount = [theDict count];
NSString *keys[theCount];
NSString *values[theCount];
// Get the keys and values from the CFDictionary. Note that because
// we're using the "GetKeysAndValues" function, you don't need to
// release keys or values. It's the "Get Rule" and only applies to
// CoreFoundation objects.
[theDict getObjects:values andKeys:keys];
// Set the address label's text.
addressid = [NSString stringWithFormat:#"%#, %#",
[theDict objectForKey:(NSString *)kABPersonAddressStreetKey],
[theDict objectForKey:(NSString *)kABPersonAddressCityKey]];
// call the js to fill the all contact infromation
{
NSString* jsString = [[NSString alloc] initWithFormat:#"fillContactAddress('%#');",addressid];
NSLog(#"jscript:%#",jsString);
[webView stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
}
[[self appViewController] dismissModalViewControllerAnimated:YES];
// If they didn't pick an address, return YES here to keep going.
return NO;
}
At this point i am getting the name,address,phone number,email address,home page,photo in NSLog console when i run the application. Following is my javascript function to retrive them:
function fillContactInfo(name,mobile,email,homepage,imgData){
mobile = mobile.replace(" ", "").replace("(", "").replace(")", "").replace("-", "").replace("+", "").replace("-", "").replace("-", "");
user.contactname = name;
user.contactemail = email;
user.contactphone = mobile;
user.contactimage = imgData;
user.contacturl = homepage;
document.getElementById("contactname").value = user.contactname;
document.getElementById("contactemail").value = user.contactemail;
document.getElementById("contactphone").value = user.contactphone;
document.getElementById("contactimage").src = "data:image/png;base64,user.contactimage";
document.getElementById("contacturl").value = user.contacturl;
}
function fillContactAddress(address){
user.address = address;
document.getElementById("contactaddress").value = user.contactaddress;
}
In the above code I have develop to retrive the contact in my application. But when i run the application at that time i only get name, email, phone number, and home page in my view. but i cant get address, and photo in my view.
I also have try to put the name, phone number, email, and home page in the
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)picker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
class method but it is not giving me result.
And I have html image tag to display image of the contact list.
The content of address inside the ABMultiValue is a NSDictionary structure. You can use the following keys to retrieve desired values. The detail is in this official document.
const CFStringRef kABPersonAddressStreetKey;
const CFStringRef kABPersonAddressCityKey;
const CFStringRef kABPersonAddressStateKey;
const CFStringRef kABPersonAddressZIPKey;
const CFStringRef kABPersonAddressCountryKey;
const CFStringRef kABPersonAddressCountryCodeKey;
About image data, I cannot get a clear picture about how it will be processed in javascript. Maybe you need to transfer those binary data with base64 manner.
Edit:
An address example:
ABMultiValueRef aMulti = ABRecordCopyValue(srcRecord, kABPersonAddressProperty);
if (aMulti != nil) {
int aMultiCount = ABMultiValueGetCount(aMulti);
for (int i = 0; i < aMultiCount; ++i) {
NSDictionary *abDict = (NSDictionary *)ABMultiValueCopyValueAtIndex(aMulti, i);
NSString *street = [abDict objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [abDict objectForKey:(NSString *)kABPersonAddressCityKey];
// obtains other properties ....
[abDict release];
}
CFRelease(aMulti);
}

iPhone address book problem

I'm having a bit of an issue with using the address book to get the names of the contacts from the device into my own contacts view within my application.
The code I have works fine on the emulator but I when tested on an iPhone 4 it will crash, the application seems to work fine if there are two or less contacts but 3 or more and the application crashes.
Here is the code I am using to get the names of contacts into an array.
ABAddressBookRef addressBook;
bool wantToSaveChanges = YES;
bool didSave;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
listOfContacts = [[NSMutableArray alloc]init];
int i;
int len = (int) ABAddressBookGetPersonCount(addressBook);
for(i = 1; i< (len+1); i++){
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, (ABRecordID) i);
NSString* name = (NSString *)ABRecordCopyCompositeName(person);
ABMultiValueRef number = (NSString *)ABRecordCopyValue(person,kABPersonPhoneProperty);
NSString *mobileNum = (NSString *)ABMultiValueCopyValueAtIndex(number, 0 );
NSLog(#"Name = %#", name);
NSLog(#"Number = %#", mobileNum);
[listOfContacts addObject:name];
[name release];
[mobileNum release];
}
if(ABAddressBookHasUnsavedChanges(addressBook)){
if(wantToSaveChanges){
didSave = ABAddressBookSave(addressBook, &error);
if(!didSave){
//Error
}
}
else{
ABAddressBookRevert(addressBook);
}
}
When it crashes this is the line that gets highlighted in Xcode:
NSString* name = (NSString *)ABRecordCopyCompositeName(person);
And the error states:
Thread 1: Program received signal: "EXC_BAD_ACCESS"
Can anyone see what the problem might be? I dont understand why it would work on the emulator but not on the device? And also why it works for up to two contacts but not 3 or more??
Just a guess:
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, (ABRecordID) i);
This line looks fishy for me. I doubt that the record IDs are numbered from 1 to whatever. Especially if you have deleted an entry.
This would explain why it works on the simulator, I guess you just added some test contacts and never deleted one.
Here is how I solved it:
(Note how I make sure to call only active records, also I created a customized Contact class)
This code takes care of edge case like: email/phone doesn't exist, or exists more than once...
+(NSArray *)getAddressBook{
ABAddressBookRef addressBook;
bool wantToSaveChanges = YES;
bool didSave;
CFErrorRef error = NULL;
addressBook = ABAddressBookCreate();
NSMutableArray *listOfContacts = [[NSMutableArray alloc]init];
CFArrayRef array=ABAddressBookCopyArrayOfAllPeople(addressBook);
int len=CFArrayGetCount(array);
for (int i = 0; i<len; i++){
ABRecordRef person = CFArrayGetValueAtIndex(array, i);
if (ABRecordGetRecordType(person)==kABPersonType){
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
ABMultiValueRef emails = (ABMultiValueRef)ABRecordCopyValue(person,kABPersonEmailProperty);
ABMultiValueRef numbers = (ABMultiValueRef)ABRecordCopyValue(person,kABPersonPhoneProperty);
int sumEmails=ABMultiValueGetCount(emails);
int sumNumbers=ABMultiValueGetCount(numbers);
for (int j=0; j<(sumNumbers>sumEmails?sumNumbers:sumEmails); j++) {
ACL_AB_Contact *contact=[[ACL_AB_Contact alloc]initWithFirstName:firstName LastName:lastName];
if (j<sumEmails){
contact.emailAddress=(NSString *)ABMultiValueCopyValueAtIndex(emails,j);
}
if (j<sumNumbers){
contact.phoneNumber=(NSString *)ABMultiValueCopyValueAtIndex(numbers,j);
}
[contact logContact];
[listOfContacts addObject:contact];
[contact release];
}
}
}
if(ABAddressBookHasUnsavedChanges(addressBook)){
if(wantToSaveChanges){
didSave = ABAddressBookSave(addressBook, &error);
if(!didSave){
//Error
}
}
else{
ABAddressBookRevert(addressBook);
}
}
return [listOfContacts autorelease];
}
The record IDs are dynamic. It means that if you add 2 contacts and then remove the first, you will have only a contact with id "2". So I wouln't use a for statement to get through the contacts. Follow the Address Book Programming Guide

how to search contact from address book using uitextview in iphone?

i'm using uitextview for searching the contact details from address book. i.e., like uisearchbar search the contact details while text edit change in uitextview.
i'm using uitextview if i tap 'a', the list of the contacts from address book will be display in table view
please give me the solution
Finally I got the solution simple coding here it is
//viewDidLoad
NSMutableArray *personArray = [[NSMutableArray alloc] init];
ABRecordRef personRef;
for (int i = 0; i < CFArrayGetCount(allPeople); i++)
{
personRef = (ABRecordID*)CFArrayGetValueAtIndex(allPeople, i);
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(personRef));
#try
{
if (ABRecordCopyValue(person, kABPersonBirthdayProperty))
[personArray addObject: (id)personRef];
}
#catch (NSException * e)
{
}
#finally
{
}
NSString *FirstName = [[[NSString stringWithString:(NSString *)ABRecordCopyValue(personRef, kABPersonFirstNameProperty)]stringByAppendingString:#" "]stringByAppendingString:(NSString *)ABRecordCopyValue(personRef, kABPersonLastNameProperty)];
[arr addObject:FirstName];
}
[arr3 addObjectsFromArray:arr];
}
//textviewdidchange
[arr3 removeAllObjects];
NSInteger counter = 0;
for(NSString *name in arr)
{
NSRange r = [name rangeOfString:txtView.text options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
[arr3 addObject:name];
}
counter++;
}
[table reloadData];
Thanks for your help

How do you get a persons phone number from the address book?

All I want to do is let the user select a number from the address book. I found the code in this question:
How to get a Phone Number from an Address Book Contact (iphone sdk)
ABMultiValueRef container = ABRecordCopyValue(person, property);
CFStringRef contactData = ABMultiValueCopyValueAtIndex(container, identifier);
CFRelease(container);
NSString *contactString = [NSString stringWithString:(NSString *)contactData];
CFRelease(contactData);
The problem is that on the second line (when running on a 3.0 device) I get the following error:
Account Manager could not find account with identifier MobileMe:rustyshelf
followed by:
Program received signal: "EXC_BAD_ACCESS".
This is all inside the picker delegate method:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
This is just one of the contacts in my address book, which is synched with Mobile Me
Edit: I think this might be a bug with the SDK, it happens for some of my contacts but not for others...
The "identifier" argument does not hold the CFIndex of the record touched. The method I use to tell which phone number a user selected is this:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonPhoneProperty) {
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
CFRelease(multiPhones);
NSString *phoneNumber = (NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
txtPhoneNumber.text = [NSString stringWithFormat:#"%#", phoneNumber];
[phoneNumber release];
}
}
}
[self dismissModalViewControllerAnimated:YES];
return NO;
}
Going off of JWDs answer, here is a safer version that uses the builtin constants and chooses the iphone number over another mobile number...
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobile=#"";
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[mobile release] ;
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[mobile release] ;
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
break ;
}
}
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
if (property == kABPersonPhoneProperty)
{
ABMultiValueRef numbers = ABRecordCopyValue(person, property);
NSString* targetNumber = (__bridge NSString *) ABMultiValueCopyValueAtIndex(numbers, ABMultiValueGetIndexForIdentifier(numbers, identifierForValue));
NSLog(#"%#", targetNumber);
}
return NO;
}
I used this question to construct my own solution. The code I'm posting is not intended as answer, merely for someone to maybe find useful. These are the simple steps for get a property. Memory management excluded.
I use this to pull the mobile number from an ABRecordRef/ The "record" variable is the ABRecordRef you want the phone number for. Where I have "" you can use another phone tag string to find other types of phone numbers.
//Get mobile phone number
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(record, kABPersonPhoneProperty);
NSString* mobile=#"";
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:#"_$!<Mobile>!$_"]) {
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
}
}
You should be able to just do the following:
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
Of course, that's only going to get you the first number of (potentially) many associated with the person who's been selected.
you can also use this "ABMultiValueGetIndexForIdentifier" like in this code :
ABPropertyType pt = ABPersonGetTypeOfProperty(property);
NSString *phoneNumber;
if ((pt & kABMultiValueMask) == kABMultiValueMask) {
ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
CFIndex idx = ABMultiValueGetIndexForIdentifier(phoneProperty, identifier);
phoneNumber = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,idx);
CFRelease(phoneProperty);
}
Going off from Dan's answer :
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonPhoneProperty) { // if tapped is equal to a phone property
CFStringRef cfnumber;
ABMultiValueRef numbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(numbers); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (numbers, i)) { //if tapped number identifier is the same as identifier number tapped
cfnumber = ABMultiValueCopyValueAtIndex(numbers, i); // copy the number to CFSTRING number
}
}
NSString *number = [NSString stringWithFormat:#"%#",cfnumber];
CFRelease(cfnumber);
//do anything you want with the number
}
return NO;
}
Can't tell you if it's the best/correct way. But i ran into some errors using Dan's code.. hence i've decided to share it after figuring it out.
Still learning objective c.. haha..
Hope it helps..
Regards,
Steve0hh