Copy contacts phone numbers to array using ABAdressPhoneBookRef - ios

I think I need to use ABMultiValueCopyArrayOfAllValues to grab all phone numbers from my ABAddressBookRef reference variable.
(If this is not the correct way to do it, please give me a correct way; ANY way that gives me access to contact phone numbers will do)
What I'd like to do now, is grab my user's contact's phone numbers(preferably their cell) and add that to an array.
How do I grab just the numbers and add that to an array?
Any help, suggestions, or advice with this is greatly appreciated in advice, I'm not finding this anywhere.
Thanks.

If I were you, I would use this approach:
NSMutableArray *allPhoneNumbers = #[].mutableCopy;
NSArray *allContact = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(book);
for (id rec in allContacts){
ABMultiValueRef mvr = ABRecordCopyValue((__bridge ABRecordRef)rec, kABPersonPhoneProperty);
NSArray *currentNums = (__bridge NSArray*) ABMultiValueCopyArrayOfAllValues(mvr);
[allPhoneNumbers addObjectsFromArray: currentNums];
}
I have not tested this, but it should work. Tell me if you have any issues.
If you want just one contact's numbers (the above gets every contact), use this code.
ABMultiValueRef mvr = ABRecordCopyValue(yourRecordRef, kABPersonPhoneProperty);
NSArray *currentNums = (__bridge NSArray*) ABMultiValueCopyArrayOfAllValues(mvr);
[allPhoneNumbers addObjectsFromArray: currentNums];

ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phoneNumbers); j++)
{
CFStringRef phoneNumberRefCF = ABMultiValueCopyValueAtIndex(phoneNumbers, j);
CFStringRef locLabelCF = ABMultiValueCopyLabelAtIndex(phoneNumbers, j);
NSString *phoneLabelCF =(NSString*) ABAddressBookCopyLocalizedLabel(locLabelCF);
NSString *phoneNumberCF = (NSString *)phoneNumberRefCF;
CFRelease(phoneNumberRefCF);
CFRelease(locLabelCF);
NSLog(#" - %# (%#)", phoneNumberCF, phoneLabelCF);
}

Related

How to check if a contact in Address Book has multiple numbers in iOS?

I have a requirement to determine if a contact that is saved in the address book has multiple mobile numbers.
I have the record ID of a Contact and I need to check if that contact has multiple mobile numbers.
Is this possible?
Got the Answer!!! I have written a method to get the total count of mobile numbers for a contact
-(NSInteger)getCountOfTotalMobileNumbersForContact:(ContactData *)contactData{
NSInteger totalCountOfMobileNumbers = 0;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABRecordRef contact = ABAddressBookGetPersonWithRecordID(addressBook, (int)contactData.mRecordID);
if(contact != nil)
{
self.phoneNumbers = ABRecordCopyValue(contact, kABPersonPhoneProperty);
NSString *lFirstName = (__bridge_transfer NSString*)ABRecordCopyValue(contact, kABPersonFirstNameProperty);
NSString *lLastName = (__bridge_transfer NSString*)ABRecordCopyValue(contact, kABPersonLastNameProperty);
NSString *fullName = lFirstName;
if (lLastName.length) {
fullName = [[lFirstName stringByAppendingString:#" "] stringByAppendingString:lLastName];
}
totalCountOfMobileNumbers = ABMultiValueGetCount(self.phoneNumbers);
}
return totalCountOfMobileNumbers;
}

How do I get Facebook and custom social addresses from iOS address book?

In my peoplePickerNavigationController:shouldContinueAfterSelectingPerson I'm able to read a bunch of the iOS Contacts info from the chosen contact into my app.
The one item I can't figure out how to read is the Facebook (and other social) address info. Here's what I've got and I know it's not quite right. The problem has something to do with the label I think:
ABMultiValueRef socialRef = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
for (int i = 0; i < ABMultiValueGetCount(socialRef); i++) {
CFStringRef currentSocialLabel = ABMultiValueCopyLabelAtIndex(socialRef, i);
CFStringRef currentSocialValue = ABMultiValueCopyValueAtIndex(socialRef, i);
if (CFStringCompare(currentSocialLabel, kABPersonSocialProfileServiceFacebook, 0) == kCFCompareEqualTo) {
[theContactInfoDictionary setObject:(__bridge NSString *)currentSocialValue forKey:#"theFacebook"];
}
CFRelease(currentSocialLabel);
CFRelease(currentSocialValue);
}
CFRelease(socialRef);
Please point me in the right direction.
Furthermore, I also need to check if there is a custom social entry for "Google+" (my app can create these entries in another place) and if there is, I want to import that value as well. How do I go about doing this?
Thanks!
sigh
And I figured it out. Here's what I did:
ABMultiValueRef socialRef = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
for (int i = 0; i < ABMultiValueGetCount(socialRef); i++) {
NSDictionary *socialDictionary = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(socialRef, i);
if ([socialDictionary[#"service"] isEqualToString:(__bridge NSString *)kABPersonSocialProfileServiceFacebook]) {
[theContactInfoDictionary setObject:(NSString *)socialDictionary[#"username"] forKey:#"theFacebook"];
} else if ([socialDictionary[#"service"] isEqualToString:(__bridge NSString *)kABPersonSocialProfileServiceTwitter]) {
[theContactInfoDictionary setObject:(NSString *)socialDictionary[#"username"] forKey:#"theTwitter"];
} else if ([socialDictionary[#"service"] isEqualToString:#"Google+"]) {
[theContactInfoDictionary setObject:(NSString *)socialDictionary[#"username"] forKey:#"theGoogle"];
}
}
CFRelease(socialRef);

stringByReplacingOccurrencesOfString only works on some objects (NSString black in debugger instead of blue)

I have a method for getting all the contacts from the iPhone contacts app and i want to add the phone numbers to an object after i have removed all the spaces in the phone number string. The problem is that this only works for some of the contacts. I have noticed that in the debugger the string-objects sometimes show in a blue color and sometimes in black. Anybody have a clue what is going on here?
Images:
Does not remove spaces in phone number
http://ctrlv.in/293692
Removes spaces in phone number
http://ctrlv.in/293691
Code:
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef sortedPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
//RETRIEVING THE FIRST NAME AND PHONE NUMBER FROM THE ADDRESS BOOK
CFIndex number = CFArrayGetCount(sortedPeople);
NSString *firstName;
NSString *phoneNumberFromContact;
for(int i = 0; i < number; i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(sortedPeople, i);
firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
phoneNumberFromContact = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phones, 0);
if(phoneNumberFromContact != NULL)
{
Contact *contact = [[Contact alloc]init];
contact.firstName = firstName;
phoneNumberFromContact = [phoneNumberFromContact stringByReplacingOccurrencesOfString:#" " withString:#""];
contact.phoneNumber = phoneNumberFromContact;
[self.contacts addObject:contact];
}
}
There are many characters that appear as spaces in NSStrings. Removing all instances of all of them is rather difficult. The best method for your case is to keep only the characters you want (numbers). As the answer you referenced states you need to create a set of characters to keep:
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"0123456789"];
Then you need to remove everything but those characters. This can be done in a few different ways. The scanner in the answer you suggested is likely the fastest. But the way with the fewest lines of code (and in my mind most readable) would be something like:
number = [[number componentsSeparatedByCharactersInSet: numbers] componentsJoinedByString: #""];
It may be slow, so if you do this a million times then keep to the scanner.

Contact number stays nil

I have an mutableArray and put the number of a contact in it.
I use the following code:
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
[thenumbers addObject:number];
In order to retrieve it, I do this:
Contacts *num = [thenumbers objectAtIndex:indexPath.row];
NSString *numbers = [NSString stringWithFormat:#"%#", [num number]];
cell.detailTextLabel.text = numbers;
I set breakpoints and it stops at the right lines. I also tried
NSLog(#"%#", number);
And it returns the numbers. And yes I have reloadDate in viewWillappear.
You're releasing phoneNumbers straight after copying to it. Try moving it to after you use it.
Or, better still, transfer the ownership to the NSString object through ARC
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
number = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
And there is no need to call CFRelease(phoneNumbers) at all.
As for getting the numbers into the cell, you're getting confused about your types. You're putting an NSString into theNumber but your pulling out a Contacts. And then sending it some kind of number message.
You've put a string in the array, you can only pull out a string or a subclass.
NSString *numbers = [thenumbers objectAtIndex:indexPath.row];
cell.detailTextLabel.text = numbers;
In the following code,
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
CFRelease(phoneNumbers);
number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
You are releasing the phoneNumbers and then you are accesing the variable. Change it to as follows.
ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
number = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
CFRelease(phoneNumbers);

Question regarding errand on UITableView creation from iPhone Addressbook

I have a question regarding the AddressBook Framework for iOS. The situation is as follows:
I'm trying recreate the contacts view from the phone application, but I want to show the contact's phone numbers in the same view. So if a contact has more than one number, his name will be in the TableView multiple times, each time with a different number.
I am trying to accomplish that by extracting all the information I need when the view loads and after that, populate the TableView with the appropriate values from an NSArray consisting of NSDictionaries containing the contact's information.
This works great except for one thing... The contact's phonenumbers and labels are read correctly and stored in the dictionary, but when I read them out later, they seem to have vanished.
Here's my code for generating the NSDictionaries, I bet it's some kind of memory management error or something completly stupid. I hope anyone can help me, thanks a lot in advance!
persons = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
for (id record in people)
{
ABMultiValueRef numbers = ABRecordCopyValue((ABRecordRef)record, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(numbers); ++i) {
CFStringRef label = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(numbers, i));
CFStringRef number = ABMultiValueCopyValueAtIndex(numbers, i);
CFStringRef firstNameRef = ABRecordCopyValue((ABRecordRef)record, kABPersonFirstNameProperty);
CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)record, kABPersonLastNameProperty);
CFDataRef imageDataRef = ABPersonCopyImageDataWithFormat((ABRecordRef)record, kABPersonImageFormatThumbnail);
NSString *firstName = [NSString stringWithFormat:#"%#", firstNameRef];
NSString *lastName = [NSString stringWithFormat:#"%#", lastNameRef];
NSString *pLabel = [[NSString alloc] initWithFormat:#"%#", label];
NSString *pNumber = [NSString stringWithFormat:#"%#", number];
UIImage *image = [UIImage imageWithData:(NSData*)imageDataRef];
NSDictionary *personDict = [NSDictionary dictionaryWithObjectsAndKeys:firstName, #"firstName", lastName, #"lastName", image, #"image", pNumber, #"phoneNumber", pLabel, #"label", nil];
NSLog(#"In %# - %#", pLabel, pNumber);
[persons addObject:personDict];
CFRelease(firstNameRef);
CFRelease(lastNameRef);
//CFRelease(imageDataRef);
CFRelease(label);
CFRelease(number);
}
}
CFRelease(addressBook);
CFRelease(source);
[people release];
Finally could resolve this myself. Apparently I was adding some nil-images to the dictionaries which they couldn't handle.

Resources