0

I have an array of contacts, when I am trying to add a contact into a group, its crash my application. Here is my code :

for (int i = 0; i < [contactArray count]; i++)
{
    ABRecordRef newPerson =  [contactArray objectAtIndex:i];
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,name, &error);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, phone,nil);
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);

    BOOL add = ABGroupAddMember(newGroup, newPerson, &error);
    if (add) {
        NSLog(@"added");
    }
}

My application crashes on ABRecordRef newPerson = [contactArray objectAtIndex:i];

How can I get the record of a person from the array to add it to a group ?

rdurand
  • 7,342
  • 3
  • 39
  • 72
user2169470
  • 33
  • 1
  • 3

2 Answers2

0

Use:

ABRecordRef newPerson = (id)[contactArray objectAtIndex:i];

A ABRecordRef is a typedef for CFTypeRef and that in turn resolves to const void *.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Use below code and try:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBookRef);

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBookRef);
for (int i=0;i < nPeople;i++) {
 ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);


        NSString *firstName, *lastName;
        ABMultiValueRef phones;
        firstName =(__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
        lastName  = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
        phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
 }
    CFRelease(allPeople);
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
Nims
  • 431
  • 6
  • 15
  • Hey its work ,but I want to add in group only selected contacts from array. it is possible with this? – user2169470 Mar 20 '13 at 10:20
  • No, ABAddressBookCopyArrayOfAllPeople will give all peoples in address book..For selection you use ABPeoplePickerNavigationController – Nims Mar 20 '13 at 10:28