[英]How can i delete contact from main AddressBook when i delete specific group in objective-c?
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.
我已经尝试过此代码,它已成功删除该组,但在主addressBook联系人上并未被删除。
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
ABAddressBookRemoveRecord(iPhoneAddressBook, newGroup, &error);
ABAddressBookSave(iPhoneAddressBook,&error);
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.
所以我已经删除了旧组并添加了从服务中提供的新联系人,但是它将是主AddressBook上联系人的两倍,因为它只会删除组。 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.. 最后我的问题是...我怎样才能从主要AddressBook删除群组和群组联系人,请帮助我...预先谢谢..
You can Do this 你可以这样做
-(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);
}
First you can check the group are exist or not if exist give the group id 首先,您可以检查该组是否存在,并提供组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;
}
Check It. 核实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.