当我删除 objective-c 中的特定组时,如何从主地址簿中删除联系人?
How can i delete contact from main AddressBook when i delete specific group in objective-c?
我想删除地址簿上的群组。我已经尝试过此代码,它已成功删除该组,但未删除主地址簿上的联系人。
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
ABAddressBookRemoveRecord(iPhoneAddressBook, newGroup, &error);
ABAddressBookSave(iPhoneAddressBook,&error);
我的要求是每次打开应用程序时我都可以调用一项服务,我可以从特定组的服务中获得联系。所以我删除了旧组并添加了服务提供的新联系人,但它会增加主地址簿上联系人的两倍,因为它只会删除组。我要删群,这个群联系人在主地址簿上都删了。
最后我的问题是...如何从主地址簿中删除组和组联系人请帮助我...提前谢谢您..
你可以这样做
-(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
-(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;
}
检查一下。
我想删除地址簿上的群组。我已经尝试过此代码,它已成功删除该组,但未删除主地址簿上的联系人。
CFErrorRef error = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
ABAddressBookRemoveRecord(iPhoneAddressBook, newGroup, &error);
ABAddressBookSave(iPhoneAddressBook,&error);
我的要求是每次打开应用程序时我都可以调用一项服务,我可以从特定组的服务中获得联系。所以我删除了旧组并添加了服务提供的新联系人,但它会增加主地址簿上联系人的两倍,因为它只会删除组。我要删群,这个群联系人在主地址簿上都删了。
最后我的问题是...如何从主地址簿中删除组和组联系人请帮助我...提前谢谢您..
你可以这样做
-(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
-(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;
}
检查一下。