1

I am getting an array of all the contacts in address book as so:

  NSMutableArray *records = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople( addressBook );

What format would a predicate be in for say the first name of a contact? I have tried record. as suggested in this question: Search ABAddressbook iOS SDK but I get an unknown key exception. The items in the array seem to be __NSCFType type. Any help would be greatly appreciated.

Community
  • 1
  • 1
John Lane
  • 1,112
  • 1
  • 14
  • 32

2 Answers2

1

hey for get contact info from device you can use my bellow code also with Contact bean class which i create just see this..

Also you need to include AddressBook.framework

#import <AddressBook/AddressBook.h>
#import <AddressBook/ABAddressBook.h>
#import <AddressBook/ABPerson.h>

[contactList removeAllObjects];

// open the default address book. 
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
    NSLog(@"opening address book");
}

// 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++) { 
 MContact *contact = [[MContact alloc] init];

 ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
 CFStringRef firstName, lastName;
 firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
 lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
 contact.name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

 ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
 if(ABMultiValueGetCount(eMail) > 0) {
  contact.email =  (NSString *)ABMultiValueCopyValueAtIndex(eMail, 0);
  [contactList addObject:contact];
 }

 CFRelease(ref);
 CFRelease(firstName);
 CFRelease(lastName);


}

and here MContact is NObject (Bean) file bellow

@interface MContact : NSObject { 
NSString *email; 
NSString *name; 
NSString *lastName; 
NSString *phone; 

BOOL isSelected; 
} 
@property (nonatomic, retain) NSString *email; 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *lastName; 
@property (nonatomic, retain) NSString *phone; 

@property (nonatomic) BOOL isSelected; 
@property (nonatomic, readonly) NSString *displayName; 
@end

i hope this help you...

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

Mario have a look at below code. I've tested it and it's working fine.

    // Retrieving the address from address book...
    ABAddressBookRef ref = ABAddressBookCreate();
    CFArrayRef getArr = ABAddressBookCopyArrayOfAllPeople(ref);
    CFIndex totCount = CFArrayGetCount(getArr);
    for (int m = 0; m < totCount; m++)
    {
        ABRecordRef recordRef = CFArrayGetValueAtIndex(getArr, m);
        ABMultiValueRef names = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
        NSString* getFirstName = (__bridge NSString *)names;
        NSLog(@"The name is :-%@\n", getFirstName);       
        getFirstName = (__bridge NSString*)ABMultiValueCopyValueAtIndex(names, 1);
        NSLog(@"The phone number is :-%@\n", getFirstName);
    }

In any concern, please get back to me.

sridvijay
  • 1,526
  • 18
  • 39
Mohit_Jaiswal
  • 840
  • 6
  • 10
  • Hi thanks for that, my question is how would you apply a predicate to the getArr – John Lane Nov 29 '12 at 13:19
  • You can't us NSPredicates with ABAddressBook – Christopher Pickslay Nov 29 '12 at 22:40
  • Hey Mario, if you want to use NSPredicate, then you need to first of all cast CFArrayRef to NSMutableArray/NSArray and since CFArrayRef is toll free bridge, so you can achieve it as: A CFArrayRef is toll-free bridged to NSArray *, so you can cast it as such and then create a mutable copy: NSMutableArray *convertedData = (__bridge NSArray*)getArr mutableCopy]; And, then you can use NSPredicate as you do with NSArray. Hope, this is what you need. :) – Mohit_Jaiswal Nov 30 '12 at 04:54