OStack程序员社区-中国程序员成长平台

标题: ios - 当我在objective-c中删除特定组时,如何从主通讯录中删除联系人? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:40
标题: ios - 当我在objective-c中删除特定组时,如何从主通讯录中删除联系人?

我想删除地址簿上的组。我尝试了此代码,它成功删除了组,但未删除主地址簿上的联系人。

CFErrorRef  error = NULL;

    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef newGroup;

    newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
    ABAddressBookRemoveRecord(iPhoneAddressBook, newGroup, &error);
    ABAddressBookSave(iPhoneAddressBook,&error);

我的要求是每次打开应用程序时我都可以调用一项服务,我可以从特定组的服务中获得联系。所以我删除了旧组并添加了从服务中提供的新联系人,但它将是主地址簿上的联系人的两倍,因为它只会删除组。我想删除组以及此组联系人在主地址簿上都被删除。

最后我的问题是...如何从主地址簿中删除组和组联系人请帮助我...提前谢谢您..



Best Answer-推荐答案


你可以这样做

-(void)RemoveContactGroupNSString *)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)CheckIfGroupExistWithNameNSString*)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;
}

检查一下。

关于ios - 当我在objective-c中删除特定组时,如何从主通讯录中删除联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569682/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4