ambuj shukla
ambuj shukla

Reputation: 43

store address book contacts in an array in iPhone

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 );

Upvotes: 2

Views: 1651

Answers (1)

Nitin Gohel
Nitin Gohel

Reputation: 49710

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

Upvotes: 1

Related Questions