【问题标题】:Objective C Adding a Contact to a specific Group in the iPhoneObjective C 将联系人添加到 iPhone 中的特定组
【发布时间】:2012-01-05 03:32:39
【问题描述】:

我正在制作一个在通讯录中存储联系人的应用程序,我使用的是 Xcode 4.2

我知道如何在通讯录中添加联系人,假设我在联系人列表中有一个名为“A”的组,我想将此联系人添加到该组,该怎么做?

这是我正在使用的代码:

ABAddressBookRef *iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef contact = ABPersonCreate();

    //add infos
    ABRecordSetValue(contact, kABPersonFirstNameProperty,(__bridge_retained CFStringRef)firstName, nil);
    ABRecordSetValue(contact, kABPersonLastNameProperty,(__bridge_retained CFStringRef)lastName, nil);
    ABRecordSetValue(contact, kABPersonOrganizationProperty, (__bridge_retained CFStringRef)organization, nil);
    ABRecordSetValue(contact, kABPersonJobTitleProperty, (__bridge_retained CFStringRef)title, nil);


    ABMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiRealPropertyType);


    ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workTel, kABWorkLabel, NULL);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge_retained CFStringRef)workFax, kABPersonPhoneWorkFAXLabel, NULL);

    ABRecordSetValue(contact, kABPersonPhoneProperty, multiPhone, nil);
    CFRelease(multiPhone);
    ABAddressBookAddRecord(iPhoneAddressBook, contact, nil);

谢谢

【问题讨论】:

    标签: iphone objective-c ios addressbook


    【解决方案1】:

    首先检查组是否存在:

        -(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); 
    

    }

    使用它来创建新组,并存储其 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);
    

    }

    如何将联系人设置为群组

                //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
        }
    

    祝你好运

    【讨论】:

    • 警告实现者,这段代码存在内存问题。
    • 非常感谢!
    【解决方案2】:

    试用代码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
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多