如何将联系人添加到特定组? IOS

时间:2013-10-04 05:20:59

标签: iphone ios ios6 ios7

使用组ID获取联系人并创建组并推送联系人inot组在ios中失败

我检查组是否已存在,如果没有可用,则创建组

-(void) CheckIfGroupExistWithName:(NSString*)groupName {


BOOL hasGroup = NO;
  //checks to see if the group is created ad creats group for 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);
    NSLog(@"GroupName= %@",groupName);
    if ([currentGroupName isEqualToString:groupName]){
        //!!! important - save groupID for later use
    groupId = ABRecordGetRecordID(currentCheckedGroup);
        NSLog(@"group id = %i",groupId);
        hasGroup=YES;
    }
}

if (hasGroup==NO){
    //id the group does not exist you can create one
    [self createNewGroup:groupName];
}

//CFRelease(currentCheckedGroup);
CFRelease(groupLists);
//CFRelease(addressBook);

}

获取联系方式并尝试以这种方式保存

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController  *)peoplePicker
    shouldContinueAfterSelectingPerson:(ABRecordRef)person
    {


NSString *name = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

contactNameText.text=name;

ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobile=@"";
mobile = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, 0);

contactNoText.text=mobile;
CFErrorRef error=NULL;
addressBook = ABAddressBookCreate();
ABRecordRef ICEBUZZGroup = ABAddressBookGetGroupWithRecordID(addressBook, groupId);
BOOL didAdd = ABGroupAddMember(ICEBUZZGroup,person,&error);

 if (!didAdd) {
}


BOOL didSave = ABAddressBookSave(addressBook, &error);

if (!didSave) {

}

PLs帮助

提前感谢

0 个答案:

没有答案