Using iOS Addressbook api to search exchange contacts - iphone

I am developing app where I would like to search for exchange contact by Name (similar to what phone contacts app does), going through AddressBook API of iOS and also searching net I am still not able to understand how can I use iOS address book api to search exchange contact.
I could only find that Address book provided information that an ABSource is searchable but does not provide how to search. If any body can help it is much appreciated. Thank you Very much in advance.. I have been struggling on this for quite a long time now..
I also tried to customize ABPeoplePicker but had no much help.

The approach I took to solving this was to find the ABSource records that I wanted, then use those to get the ABPerson records in the source, then build up some data structures and filter them with NSPredicate. A bit convoluted perhaps but it seems to work.
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef sources = ABAddressBookCopyArrayOfAllSources(addressBook);
CFIndex sourcesCount = CFArrayGetCount(sources);
ABRecordRef sourceToSearch = NULL;
for (CFIndex index = 0; index < sourcesCount; index++)
{
ABRecordRef record = (ABRecordRef)CFArrayGetValueAtIndex(sources, index);
NSNumber *sourceTypeNumber = (__bridge NSNumber *)(CFNumberRef)ABRecordCopyValue(record, kABSourceTypeProperty);
ABSourceType sourceType = [sourceTypeNumber intValue];
if (sourceType == 4) //this was the only source type with people on my phone, I guess you'll use kABSourceTypeExchange instead
{
sourceToSearch = record;
break;
}
}
CFArrayRef peopleInRecord = (CFArrayRef)ABAddressBookCopyArrayOfAllPeopleInSource(addressBook, sourceToSearch);
CFIndex peopleCount = CFArrayGetCount(peopleInRecord);
NSMutableArray *peopleDictionaries = [NSMutableArray array];
for (CFIndex index = 0; index < peopleCount; index++)
{
ABRecordRef personRecord = CFArrayGetValueAtIndex(peopleInRecord, index);
ABRecordID recordID = ABRecordGetRecordID(personRecord);
NSString *personName = (__bridge NSString *)(CFStringRef)ABRecordCopyValue(personRecord, kABPersonFirstNameProperty);
if (personName)
{
NSDictionary *personDictionary = #{ #"recordID" : [NSNumber numberWithInt:recordID], #"name" : personName };
[peopleDictionaries addObject:personDictionary];
}
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K like %#",#"name",#"Kyle"];
NSArray *kyles = [peopleDictionaries filteredArrayUsingPredicate:predicate];
NSLog(#"filtered dictionarys = %#",kyles);
/*
2012-08-27 17:26:24.679 FunWithSO[21097:707] filtered dictionaries = (
{
name = Kyle;
recordID = 213;
}
)*/
//From here, get the recordID instance and go get your ABPerson Records directly from the address book for further manipulation.
Hope this helps, let me know if you have any questions!

Related

how to get records of contacts from NSMutableArray in iPhone

I have an array of dictionary for contact details. I am trying to add that record in ABRecordRef, but I don't understand how it works. Here is my code:
for (int i = 0; i <= [contactArray count]; i++)
{
ABRecordRef person = (ABRecordRef)[contactArray objectAtIndex:i];
ABAddressBookAddRecord(addressBook, group, &error);
ABAddressBookSave(addressBook, &error);
}
I am trying to add this contact records into group using ABGroupAddMember. Now how can I get the records from NSMutableArray. Any help will be greatly appreciated. Thank you.
there is no built-in functionality for that. you will have to create an empty record, THEN get all the fields from the dict and THEN add those to the record and safe it
that's annoying manual work... :D
I'd go with Erica's ABContactHelper: https://github.com/erica/ABContactHelper
then it is only
for(NSDictionary *d in recordArray) {
ABContact *contact = [ABContact contactWithDictionary:d];
ABGroupAddMember(theGroup, contact.record);
}
if you like manual:
for(NSDictionary *d in recordArray) {
ABRecordRef person = ABPersonCreate();
for(NSString *k in d.allKeys) {
id v = d[k];
//HERE call ABRecordSetValue with the right params depending on if the value is a NSString or NSArray or an image
}
ABGroupAddMember(theGroup, contact.record);
CFRelease(person);
}
disclaimer: typed inline but should be ok

ABRecordCopyValue return 0 ?

What I trying to do, is get all contacts and relies if the contact have SocialProfileProperty or not, after get all this contacts that have SocialProfileProperty I will make simple filtration to know if it is Facebook,Twitter,..
Before I post this question I west full day trying to find any solution for this problem ??
But it is allows return 0 ??
ABAddressBookRef addressBook;
CFErrorRef *erer = NULL;
addressBook = ABAddressBookCreateWithOptions(NULL, erer);
CFArrayRef _allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex _nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *socialArray = [[NSMutableArray alloc] init];
ABMultiValueRef socialProfiles;
for (int i=0;i<_nPeople; i++) {
ABRecordRef _person=CFArrayGetValueAtIndex( _allPeople,i);
NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(_person)];
NSLog(#"%#",recordId);
socialProfiles = ABRecordCopyValue(_person, kABPersonSocialProfileProperty);
if (ABMultiValueGetCount(socialProfiles)>0) {
[socialArray addObject:(__bridge id)(socialProfiles)];
}
}
NSLog(#"%d",socialArray.count);
It is allows return 0 ,until the user marge the Social contacts with native and this does
not happened until he try to delete the Facebook account for example in that case alert appear
and ask if u wanna to marge contacts or not ??? other wise return 0 values :)
So this method will never work until the user marge the FaceBook Contacts with native contacts.

Getting twitter user from Address Book

I'm trying to get the Twitter username from a contact in the address book but I'm not able to do it
I'm using this code that I found "googling":
- (NSString*)getTwitterUsernameFromRecord:(ABRecordRef)record {
NSString * twitterUsername = nil;
ABMultiValueRef socials = ABRecordCopyValue(record, kABPersonSocialProfileProperty);
if (!socials) {
return nil;
}
CFIndex socialsCount = ABMultiValueGetCount(socials);
for (int k=0 ; k<socialsCount ; k++) {
CFDictionaryRef socialValue = ABMultiValueCopyValueAtIndex(socials, k);
if(CFStringCompare( CFDictionaryGetValue(socialValue, kABPersonSocialProfileServiceKey), kABPersonSocialProfileServiceTwitter, 0)==kCFCompareEqualTo) {
twitterUsername = (NSString*) CFDictionaryGetValue(socialValue, kABPersonSocialProfileUsernameKey);
}
CFRelease(socialValue);
if(twitterUsername)
break;
}
CFRelease(socials);
return twitterUsername;
}
I've put the if to validate the "socials" array is nil because I was getting exception when trying to get "socials" array count.
I've tried this code in the simulator and in a real device with contacts which twitter info is filled but the "socials" array I get is always nil. I'm getting the wrong property from the record? Any help?
Thank you in advance
your function works for me almost as-is, aside from needing to bridge cast the twitter username (which is just an ARC thing). how are you accessing the address book?
when i use this code to call your function:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex contactCount = ABAddressBookGetPersonCount(addressBook);
for( int i = 0; i<contactCount; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
NSString *twitterHandle = [self getTwitterUsernameFromRecord:ref];
if (twitterHandle) {
NSLog(#"record %d has twitter name %#", i, twitterHandle);
}
}
i get this console output:
2012-06-26 12:09:40.864 AddressBookTest[2246:707] record 57 has twitter name alexshepard

How to use ABAddressBookRef for iPhone application?

I want reference of my address book in one of iPhone application. I have used below code. But its showing [people count] = 0.
I have 3-4 contacts on my addressbook application still [people count] returns zero.
Is anything more, I am missing here?
Code is:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSMutableArray *people = [[[(NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook) autorelease] mutableCopy]autorelease];
[people sortUsingFunction:(int (*)(id,id,void*)) ABPersonComparePeopleByName context:(void*)ABPersonGetSortOrdering()];
NSLog(#"count..%i",[people count]);
for(int i = 0 ; i < [people count]; i++){
ABRecordRef person = [people objectAtIndex:i];
//get phone no fill phone no into array
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
if(multi!=NULL && ABMultiValueGetCount(multi)>0) {
NSLog(#"in value..");
}
else {
NSLog(#"no value");
}
}
Thank You
try that -
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people;
people = ABAddressBookCopyArrayOfAllPeople(HiByeGroup);
if (people){
CFArrayCreateMutableCopy(kCFAllocatorDefault,CFArrayGetCount(people),people);
NSInteger numberOfPersonsInAB=CFArrayGetCount(peopleMutable);
for (int i =0; i< numberOfPersonsInAB; i++) {
//your code
}
CFRelease(people);
UPdate
As Jamon Holmgren noted, this answer is old and ABAddressBookCreate() is deprecated in iOS6. You need to use ABAddressBookCreateWithOptions() instead.
GOOD LUCK
Just follow given code over here. A perfect example to interact with ABAddressBook.

Issue regarding retrieving all email id's from address book in iphone

I am newbie to Address book programming. I want to retrieve all email id's from address book.The issue is below code gets the all data for one record(one person). but When i add more than one contact in address book. it crushes without showing any exception.
Any suggestions? Thanks in advance.
self.pastUrls = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
// you could probably do some kind of enumeration but I'm doing old fashoined way
int i;
for(i = 0; i < [addresses count]; i++) {
ABRecordRef record = [addresses objectAtIndex:i];
ABMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonEmailProperty);
NSLog(#"%#",multiValue);
int count = ABMultiValueGetCount(multiValue);
NSLog(#"%d",count);
int j;
for(j = 0; j < count; j++) {
NSString *label = (NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multiValue, i));
NSString *value = (NSString *)ABMultiValueCopyValueAtIndex(multiValue, i);
//NSLog(#"Email for %#: %#", label, value);
[pastUrls addObject:value];
}
}
Regards,
sathish
There are a couple of online tutorials here that should help:
http://iphone.zcentric.com/2008/09/19/access-the-address-book/
https://developer.apple.com/iphone/library/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/100-Introduction/Introduction.html
Apple's Address Book Programming Guide for iOS includes a sample project that will get you started with general principles for accessing address book data, including email addresses.