2

I'm using below coding to get first name in string format but want to get this in an array as it includes both address book contacts and facebook contact. So that I can retrieve them like array[0],array[1] and soon.So plz help me to get this done.

    ABAddressBookRef ab=ABAddressBookCreate();

    NSArray *arrTemp=(NSArray *)ABAddressBookCopyArrayOfAllPeople(ab);

    NSMutableArray *arrContact=[[NSMutableArray alloc] init];
       for (int i=0;i<[arrTemp count];i++) 
             {
    NSMutableDictionary *dicContact=[[NSMutableDictionary alloc] init];
    NSString *str=(NSString *) ABRecordCopyValue([arrTemp objectAtIndex:i],             kABPersonFirstNameProperty);
    @try
        {
    [dicContact setObject:str forKey:@"name"];
        }
    @catch (NSException * e) {
    [dicContact release];
    continue;
     }

    [dicContact release];

    NSLog(@"%@",str );
NANNAV
  • 4,875
  • 4
  • 32
  • 50

1 Answers1

1

you can store Your addressBook all contacts in to NSMutableArray like bellow:-

ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray* allPeoplesDicts = [NSMutableArray array];
for (id person in thePeople)
{
    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* name = (NSString *)ABRecordCopyCompositeName(person);
    NSMutableArray* phones = [[NSMutableArray alloc] init];
    for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
    {
        NSString *phone = [(NSString *)ABMultiValueCopyValueAtIndex(phones,i) autorelease];
        [phones addObject:phone];
    }
    NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys:name,@"Name",phones,@"PhoneNumbers",nil];
    [phones release];
    [allPeoplesDicts addObject:personDict];
    [personDict release];
}

i just Google it and i got this visit this similar questions:-

how can I Add a ABRecordRef to a NSMutableArray in iPhone?

storing adressbook contacts into a nsdictionary

Community
  • 1
  • 1
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • Thanks Nitin for the answer. – ambuj shukla Jan 12 '13 at 09:50
  • Ya I have tick right and for upvoting it is saying reputation must be atleast 15 – ambuj shukla Jan 12 '13 at 10:41
  • I have taken the name in to array and it giving output in console by doing nslog as 2013-01-12 16:15:58.955 Birthday_Reminder[1757:c07] ( "Salil Pachori" ) and by printing that array into nslog(@"%@",array[1]) it is showing error so how can I do that so it comes in array[1],array[2] form – ambuj shukla Jan 12 '13 at 10:49