How to retrieve mobile numbers from iPhone Contacts. - iphone

I use the following code to set the retrieve the phone number in my app.
CFStringRef addressBookMobile;
ABRecordRef person;
NSString *mobile;
person = CFArrayGetValueAtIndex(people, i);
addressBookMobile = ABRecordCopyValue(person, kABPersonPhoneProperty);
mobile = [NSString stringWithFormat:#"%#", addressBookMobile];
The tag of the contacts is 'mobile'. However, when I use the NSLog(#"%#", mobile); . It displays the <NSCFType: 0x802ffc0>. does any problem for my code?
Should I use the const CFStringRef kABPersonPhoneMobileLabel and how to use? As if I replace it as the above code, it has the error. Can anyone help me? Thank you.

check the ABPerson Refrence and You need't use #"$!!$" but kABPersonPhoneMobileLabel. the example is:
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobile=#"";
NSString* mobileLabel;
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
//NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
//NSLog(#"%#", phone);
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
NSLog(#"mobile:");
} else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
NSLog(#"iphone:");
} else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
NSLog(#"pager:");
}
[mobile release];
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(#"%#", mobile);
}

The phone numbers of a person in the Address Book are in the form of a multi-value property.
In your case, you should have something like the following (haven't tried it, typing directly here, so I don't know if it compiles and/or works):
ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *mobileNumber;
NSString *mobileLabel;
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
mobileLabel = (NSString *)ABMultiValueCopyLabelAtIndex(mobilePhones, i);
if ([mobileLabel isEqualToString:#"mobile"]) {
mobileNumber = (NSString*)ABMultiValueCopyValueAtIndex(mobilePhones,i);
break;
}
}

ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
CRStringRef mobileNumber;
CRStringRef mobileLabel;
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
mobileLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
if ([mobileLabel isEqualToString:#"_$!<Mobile>!$_"]) {
mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
break;
}
}

This is solid for ARC 64bit iOS8:
- (NSArray *)phoneNumbersOfContactAsStrings:(ABRecordRef)contactRef {
NSMutableArray *mobilePhones = [NSMutableArray arrayWithCapacity:0];
ABMultiValueRef phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty);
NSArray *allPhoneNumbers = (NSArray *)CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones));
for (NSUInteger i=0; i < [allPhoneNumbers count]; i++) {
if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
[mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))];
}
if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) {
[mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))];
}
}
CFRelease(phones);
return mobilePhones;
}

Related

fetching only mobile section record (not from iphone, home label etc) from addressbook?

How do I fetch only mobile section record from addressbook in ios?
I want to add only one record to my array that is fetched from mobile section record.
How do I do that. I am getting all the phone property records. but I need to get only mobile section record.
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);
NSLog(#"allpeople%#", allPeople);
for (id record in allPeople) {
CFTypeRef phoneProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonPhoneProperty);
NSArray *phones = (NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
NSLog(#"phones %#",phones);
CFRelease(phoneProperty);
NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init];
NSMutableString *newPhone = [[NSMutableString alloc] init];
for (NSString *phone in phones) {
if(![newPhone isEqualToString:#""])
[newPhone appendString:#", "];
[newPhone appendString:phone];
}
You can try something like this..
ABMultiValueRef phoneNumbers = ABRecordCopyValue(abPerson, kABPersonPhoneProperty);
if (phoneNumbers) {
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phone = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
if (label) {
if (CFEqual(label, kABPersonPhoneMobileLabel)) {
primaryPhoneText.text = phone;
} else {
}
CFRelease(label);
[allPhones addObject:phone]; // allPhones is an array here.
}
}
CFRelease(phoneNumbers);
}
Hope this will help you out..
Enjoy coding..

Fix Potential leak of an object

When i used build and analyse, i got leaks( it showed as potential leak of an object). to fix that i included as below
if ( aContactfirstName){
CFRelease(aContactfirstName);
}
if (aContactLastName){
CFRelease(aContactLastName);
}
But my app crashes.
So pls let me know where it leaks and solve it.
-(NSString*)getContactNameByPhoneNo:(NSString*)phoneNO{
NSString *aContactName = phoneNO;
ABAddressBookRef addressbook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
for (int i=0; i < numPeople; i++) {
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numPhones = ABMultiValueGetCount(phonelist);
for (int j=0; j < numPhones; j++) {
CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, j);
NSString *personPhone = (NSString *)ABphone;
NSLog(#"i am:");
personPhone =[personPhone stringByReplacingOccurrencesOfString:#"-"withString:#""];
personPhone=[personPhone stringByReplacingOccurrencesOfString:#")"withString:#""];
personPhone=[personPhone stringByReplacingOccurrencesOfString:#" "withString:#""];
personPhone=[personPhone stringByReplacingOccurrencesOfString:#"("withString:#""];
personPhone=[personPhone stringByReplacingOccurrencesOfString:#"+"withString:#""];
NSLog(#"xcxcxcxc");
CFRelease(ABphone);
if ( [personPhone isEqualToString:phoneNO] ){
NSString *aContactfirstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) ;
NSString *aContactLastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) ;
if ( aContactLastName != NULL && aContactfirstName != NULL){
aContactName = [NSString stringWithFormat:#"%# %#",aContactfirstName,aContactLastName];
}
else if(aContactfirstName != NULL){
aContactName = aContactfirstName;
}
else if(aContactLastName != NULL){
aContactName = aContactLastName;
}
if ( aContactfirstName){
CFRelease(aContactfirstName);
}
if (aContactLastName){
CFRelease(aContactLastName);
}
break;
}
}
CFRelease(phonelist);
}
CFRelease(allPeople);
CFRelease(addressbook);
return aContactName;
}
Use -
NSString *aContactfirstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
NSString *aContactLastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) autorelease];
if(aContactLastName != NULL){
aContactName = aContactLastName;//aContactName pointing to aContactLastName
}
In this you are assigning aContactLastName to aContactName(aContactLastName and aContactName are pointing to same memory location). and after that you are releasing aContactLastName.
if (aContactLastName){
CFRelease(aContactLastName);
}
and then you are returning return aContactName; (aContactName is already released)
this is wrong.
remove
if (aContactLastName){
CFRelease(aContactLastName);
}
from your code and return [aContactName autorelease];

Find out memory leak?

i am new on iphone apps.Now this is my first app,app is installed but not run?
I write this code it shows memory leak.please find out.Thanks in advance.
ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
CFStringRef *firstName = (CFStringRef *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(#"Name %#", firstName);
contact.strFirstName = (NSString*)firstName;
CFStringRef *lastName = (CFStringRef *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSLog(#"Name %#", lastName);
contact.strLastName = (NSString*)lastName;
contact.contactName = [NSString stringWithFormat:#"%# %#",(NSString *)firstName,lastName];
NSLog(#"Name %#", contact.contactName);
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phoneNumbers); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phoneNumbers, j);
NSString *phoneNumber = (NSString *) phoneNumberRef;
contact.strMobileNo = phoneNumber;
NSLog(#"phoneNO is %#", phoneNumber);
CFRelease(phoneNumberRef);
}
ABMultiValueRef emails = ABRecordCopyValue(ref, kABPersonEmailProperty);
for(CFIndex k = 0; k < ABMultiValueGetCount(emails); k++)
{
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(emails, k);
NSString *mailid = (NSString *) emailRef;
contact.strMail = mailid;
NSLog(#"Email is %#", mailid);
CFRelease(emailRef);
}
CFRelease(emails);
CFRelease(phoneNumbers);
You are using ABRecordCopyValue on firstName and lastName which means you need to CFRelease those as well.
CFRelease is a way to go (as #Joe and #jamapag already answered).. I would just like to add that XCode has few nice features like cmd + shift + a gives u a static memory analyser.. And you can also use run -> run w/ performance tools and then use allocations or leaks to analyse our memory management dynamically.
You need to add:
CFRelease(firstName);
CFRelease(lastName);

How to search the iphone address book for a specific phone number?

I am developing an app that connects to another iphone using bonjour. One of its features is when I connect to the other device it will automatically check if I have the other persons phone number. So my problem is how do I check my address book for the phone number provided by the other device?
Here's an example extracted from one of my address book methods. I wasn't searching by phone number but this gives you an idea have how to move forward with what you need:
- (void) scanAddressBookSample
{
NSUInteger i;
NSUInteger k;
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
if ( people==nil )
{
NSLog(#"NO ADDRESS BOOK ENTRIES TO SCAN");
CFRelease(addressBook);
return;
}
for ( i=0; i<[people count]; i++ )
{
ABRecordRef person = (ABRecordRef)[people objectAtIndex:i];
//
// Phone Numbers
//
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );
for ( k=0; k<phoneNumberCount; k++ )
{
CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel ); // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
// Find the ones you want here
//
NSLog(#"-----PHONE ENTRY -> %# : %#", phoneNumberLocalizedLabel, phoneNumberValue );
CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberLabel);
CFRelease(phoneNumberValue);
}
}
[people release];
CFRelease(addressBook);
}
-(void)createQuickAccessContacts{
NSMutableDictionary contactDictionary= [[NSMutableDictionary alloc]init];
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex n = ABAddressBookGetPersonCount(addressBook);
NSDate *date=[NSDate date];
for( int i = 0 ; i < n ; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;
[contactDictionary setObject:(__bridge id)(ref) forKey:phoneNumber];
}
}
NSLog(#" Time taken %f for %i contacts",[[NSDate date] timeIntervalSinceDate:date],[contactDictionary count]);
}
This takes like 0.5 sec for 2.5k contacts to fill
then you can find the contact using number
ABRecordRef ref= [contactDictionary objectForKey:#"89xxxxxxx"];
its super fast takes like 0.000x seconds
Use This. this is my code.
NSLog(#"=====Make People Array with Numbers. Start.");
peopleWithNumber = [[NSMutableDictionary alloc] init];
for (int i=0; i < [people count]; i++) {
NSInteger phoneCount = [self phoneCountAtIndex:i];
if (phoneCount != 0) {
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
for (int j=0 ; j < phoneCount ; j++) {
[phoneNumbers addObject:[self phoneNumberAtIndex:i phoneIndex:j]];
}
[peopleWithNumber addEntriesFromDictionary:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSArray arrayWithArray:phoneNumbers], [self fullNameAtIndex:i], nil]];
}
}
NSLog(#"=====Make People Array with Numbers. End.\n");
search method. it would be faster than using array
"NSArray *people = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);"
- (NSArray *)searchNamesByNumber:(NSString *)number {
NSString *predicateString = [NSString stringWithFormat:#"%#[SELF] contains '%#'",#"%#",number];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:predicateString,peopleWithNumber,number];
NSArray *names = [[peopleWithNumber allKeys] filteredArrayUsingPredicate:searchPredicate];
return names;
}

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