Objective C Adding a Contact to a specific Group in the iPhone

前端 未结 2 1783
陌清茗
陌清茗 2021-01-01 05:09

I am making an App that stores contacts in the address book , I am using Xcode 4.2

I know how to add a contact in the address book , let s say I have a group called

相关标签:
2条回答
  • 2021-01-01 05:39

    Try out the code originally posted by malinois:

    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record 
    ABRecordRef person = ABPersonCreate(); // create a person  
    
    NSString *phone = @"0123456789"; // the phone number to add  
    
    //Phone number is a list of phone number, so create a multivalue  
    ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty); 
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,phone,kABPersonPhoneMobileLabel, NULL);
    
    ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil); // first name of the new person 
    ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil); // his last name 
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property 
    ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record
    
    ABRecordRef group = ABGroupCreate(); //create a group 
    ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group's name 
    ABGroupAddMember(group, person, &error); // add the person to the group         
    ABAddressBookAddRecord(addressBook, group, &error); // add the group   
    
    ABAddressBookSave(addressBook, nil); //save the record  
    
    CFRelease(person); // relase the ABRecordRef  variable
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-01 05:40

    First Check if the group exist:

        -(void) CheckIfGroupExistWithName:(NSString*)groupName {
    
    
    BOOL 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 = (NSString *)ABRecordCopyCompositeName(currentCheckedGroup);
    
            if ([currentGroupName isEqualToString:groupName]){
                          //!!! important - save groupID for later use
                self.groupId = ABRecordGetRecordID(currentCheckedGroup);
                hasGroup=YES;
            }
            [groupName release];
        }
    
        if (hasGroup==NO){
                        //id the group does not exist you can create one
            [self createNewGroup:groupName];
        }
    
    //CFRelease(currentCheckedGroup);
        CFRelease(groupLists);
    CFRelease(addressBook); 
    

    }

    Use this to create new group, and store its ID

    -(void) createNewGroup:(NSString*)groupName {
    
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef newGroup = ABGroupCreate();
    ABRecordSetValue(HiByeGroup, kABGroupNameProperty,groupName, nil);
    ABAddressBookAddRecord(addressBook, newGroup, nil);
    ABAddressBookSave(addressBook, nil);
    CFRelease(addressBook);
    
    //!!! important - save groupID for later use
    self.groupId = ABRecordGetRecordID(newGroup);
    CFRelease(newGroup);
    

    }

    The is how to set a contact to a group

                //Use the Group ID you stored.
                ABRecordRef HiByeGroup = ABAddressBookGetGroupWithRecordID(addressbook, self.groupId);
        BOOL didAdd = ABGroupAddMember(HiByeGroup,ref,&error);
    
        if (!didAdd) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error while adding person to HiBye group %@", &error);
            exit(-1);  // Fail
        }
    
        BOOL didSave = ABAddressBookSave(addressbook, &error);
    
        if (!didSave) {
        // Update to handle the error appropriately.
            NSLog(@"Unresolved error while saving address book%@", &error);
            exit(-1);  // Fail
        }
    

    Good luck

    0 讨论(0)
提交回复
热议问题