ios - 当我在objective-c中删除特定组时,如何从主通讯录中删除联系人?
<p><p>我想删除地址簿上的组。我尝试了此代码,它成功删除了组,但未删除主地址簿上的联系人。 </p>
<pre><code>CFErrorReferror = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
ABAddressBookRemoveRecord(iPhoneAddressBook, newGroup, &error);
ABAddressBookSave(iPhoneAddressBook,&error);
</code></pre>
<p>我的要求是每次打开应用程序时我都可以调用一项服务,我可以从特定组的服务中获得联系。所以我删除了旧组并添加了从服务中提供的新联系人,但它将是主地址簿上的联系人的两倍,因为它只会删除组。我想删除组以及此组联系人在主地址簿上都被删除。</p>
<p>最后我的问题是...如何从主地址簿中删除组和组联系人请帮助我...提前谢谢您.. </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你可以这样做</p>
<pre><code>-(void)RemoveContactGroup:(NSString *)name {
BOOL flag = ;
if(!flag)
{
return;
}
CFErrorReferror = NULL;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newGroup;
//if Existing Group
newGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook,groupId);
NSArray *member = (__bridge NSArray *)ABGroupCopyArrayOfAllMembers(newGroup);
int nPeople = (int);
if (nPeople>0)
{
for (int i=0; i<nPeople; i++)
{
ABRecordRef contactPerson = (__bridgeABRecordRef)member;
ABAddressBookRemoveRecord(iPhoneAddressBook, (ABRecordRef)contactPerson, &error);
}
}
ABAddressBookSave(iPhoneAddressBook, NULL);
CFRelease(newGroup);
}
</code></pre>
<p>首先您可以检查组是否存在,如果存在则给出组 id</p>
<pre><code>-(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 (){
//!!! 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;
}
</code></pre>
<p>检查一下。</p></p>
<p style="font-size: 20px;">关于ios - 当我在objective-c中删除特定组时,如何从主通讯录中删除联系人?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/35569682/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/35569682/
</a>
</p>
页:
[1]