iPhone添加删除联系人组
1,添加AddressBook.framework
2,添加联系人群组
View Code
- (void)addGroup { //Create group ABAddressBookRef ab = ABAddressBookCreate(); CFErrorRef error; ABRecordRef group = ABGroupCreate(); ABRecordSetValue(group, kABGroupNameProperty,@"new group", &error); ABAddressBookAddRecord(ab, group, &error); ABAddressBookSave(ab, &error); //Create new person and save to this group ABRecordRef record = ABPersonCreate(); BOOL isSuccess ; isSuccess = ABRecordSetValue(record, kABPersonNicknameProperty,@"GroupMember nick name", &error); isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, @"Middle name", &error); ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty); CFTypeRef phone= CFSTR("123000222111"); ABMultiValueAddValueAndLabel(copyOfPhones, phone,kABPersonPhoneMobileLabel,NULL); isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error); isSuccess = ABAddressBookAddRecord(ab, record, &error); isSuccess = ABAddressBookSave(ab, &error); ABGroupAddMember(group, record, &error); NSLog(@"is success %d", isSuccess); ABAddressBookSave(ab, &error); CFRelease(group); }
3,获取联系人群组
View Code
- (NSArray *)getGroups { NSMutableArray *groupNames = [[[NSMutableArray alloc] init] autorelease]; ABAddressBookRef ab = ABAddressBookCreate(); NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllGroups(ab); for (int i = 0 ; i < [array count]; i++) { ABRecordRef group = [array objectAtIndex:i]; CFTypeRef groupName = ABRecordCopyValue(group, kABGroupNameProperty); NSString *groupNameStr = [NSString stringWithFormat:@"%@", (NSString *)groupName]; NSLog(@"groupNames is %@", groupNameStr); [groupNames addObject:groupNameStr]; CFRelease(groupName); } return groupNames; }
4,删除联系人群组
View Code
//delete all groups - (void) DeleteGroups{ ABAddressBookRef ab = ABAddressBookCreate(); CFErrorRef error; //get all groups CFArrayRef groups=ABAddressBookCopyArrayOfAllGroups(ab); //groups' count CFIndex groupCount=ABAddressBookGetGroupCount(ab); for (int i=0; i< groupCount;i++){ ABRecordRef group=CFArrayGetValueAtIndex(groups, i); //delete ABAddressBookRemoveRecord(ab, group, &error); ABAddressBookSave(ab, &error); CFRelease(groups); }
5,关于联系人的操作可以参见我的文章:iPhone通讯录联系人的增加、删除、修改的操作
6,本文写作的参考文章:iphone地址本操作,ABAddressBook删除联系人与群组