在iphone中实现地址簿

时间:2012-02-29 07:48:50

标签: iphone addressbook

在我的应用程序中,我需要创建用于发送组短信的联系人组。首先创建翻译并添加成员。问题是新添加的联系人被添加到所有联系人列表两次并且一次添加到该联系人我需要将该联系人保存到组和所有联系人列表一次。我的代码有什么问题?我使用以下代码。

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{}

调用此方法时,新输入的联系人将添加到所有联系人列表中。 然后创建一个地址簿实例addressBook = ABAddressBookCreate();我需要保存到该组的人是用户从界面添加的联系人。所以此人被设置为显示的人。

displayedPerson=person;
person = ABPersonCreate();
CFErrorRef error = NULL;

CFStringRef firstName, lastName; 
firstName = ABRecordCopyValue(displayedPerson, kABPersonFirstNameProperty); 
lastName  = ABRecordCopyValue(displayedPerson, kABPersonLastNameProperty);  
NSString *fname=(NSString *)firstName;
NSString *lname=(NSString *)lastName;


ABRecordSetValue(person, kABPersonFirstNameProperty,fname, &error);
ABRecordSetValue(person, kABPersonLastNameProperty,lname, &error);

ABAddressBookAddRecord(addressBook, person, &error);
ABAddressBookSave(addressBook, &error);

parentGroup = ABAddressBookGetGroupWithRecordID(addressBook,groupID);
ABAddressBookAddRecord(addressBook,  parentGroup, &error); 
ABAddressBookSave(addressBook, &error);

ABGroupAddMember(parentGroup, person, &error);
ABAddressBookSave(addressBook, nil);`
CFRelease(person);

但是这样做后,联系人会再次添加到所有联系人列表中。任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

  - (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
    {
        CFErrorRef error=NULL;
        ABRecordRef parentGroup = ABAddressBookGetGroupWithRecordID(newPersonViewController.addressBook,appdelegate.default_Group);//set selected group id
        ABGroupAddMember(parentGroup, person,&error);
        ABAddressBookSave(newPersonViewController.addressBook, &error);
        ABRecordID rcId = ABRecordGetRecordID(person);
         ABAddressBookSave (newPersonViewController.addressBook, &error);

        [newPersonViewController dismissModalViewControllerAnimated:YES];
    }