当我删除objective-c中的特定组时,如何从主AddressBook中删除联系人? [英] How can i delete contact from main AddressBook when i delete specific group in objective-c?
问题描述
我想删除addressBook上的组。我已经尝试此代码它成功删除该组但在主要地址簿联系人不会被删除。
i want to delete the group on addressBook. i have try this code it is successfully delete the group but on main addressBook contact are not deleted.
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
ABAddressBookRemoveRecord(iPhoneAddressBook, newGroup, &error);
ABAddressBookSave(iPhoneAddressBook,&error);
我的要求是我每次申请开通时都有一项服务可以通过服务获得联系在特定的群体中。所以我删除旧组并添加从服务提供的新联系人,但它将在主AddressBook上联系两次,因为它只会删除组。我想删除组以及此组联系人都在主要地址簿上删除。
my requirement is i have one service to call every time when application is open i have get contact from service in a specific group. so i have delete old group and add new contact which is provide from service but it will twice the contact on main AddressBook because it will only delete group. i want to delete group as well as this group contact are delete on main addressBook both.
最后我的问题是...如何删除组和组联系人主要地址簿请帮帮我...提前谢谢..
Finally my question is... How can i delete the group and group contact from main AddressBook please help me... Thank you in advance..
推荐答案
你可以这样做
-(void)RemoveContactGroup:(NSString *)name {
BOOL flag = [self CheckIfGroupExistWithName:name];
if(!flag)
{
return;
}
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
//if Existing Group
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
NSArray *member = (__bridge NSArray *)ABGroupCopyArrayOfAllMembers(newGroup);
int nPeople = (int)[member count];
if (nPeople>0)
{
for (int i=0; i<nPeople; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)member[i];
ABAddressBookRemoveRecord(iPhoneAddressBook, (ABRecordRef)contactPerson, &error);
}
}
ABAddressBookSave(iPhoneAddressBook, NULL);
CFRelease(newGroup);
}
首先,如果存在,则可以检查组是否存在给组ID
First you can check the group are exist or not if exist give the group id
-(BOOL)CheckIfGroupExistWithName:(NSString*)groupName {
hasGroup = NO;
//checks to see if the group is created ad creats group for HiBye contacts
ABAddressBookRef addressBook = ABAddressBookCreate();
CFIndex groupCount = ABAddressBookGetGroupCount(addressBook);
CFArrayRef groupLists= ABAddressBookCopyArrayOfAllGroups(addressBook);
for (int i=0; i<groupCount; i++) {
ABRecordRef currentCheckedGroup = CFArrayGetValueAtIndex(groupLists, i);
NSString *currentGroupName = (__bridge NSString *)ABRecordCopyCompositeName(currentCheckedGroup);
if ([currentGroupName isEqualToString:groupName]){
//!!! important - save groupID for later use
groupId = ABRecordGetRecordID(currentCheckedGroup);
hasGroup=YES;
}
CFRelease(currentCheckedGroup);
}
if (hasGroup==NO){
//id the group does not exist you can create one
}
return hasGroup;
}
检查。
这篇关于当我删除objective-c中的特定组时,如何从主AddressBook中删除联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!