2011-04-20 131 views
1

問候,iPhone到現有組添加聯繫人地址簿中

我在iPhone上的應用程序,與地址簿聯繫人工程工作。我試圖在聯繫人中獲取make組,但問題是當我再次運行應用程序時,組又被重新創建,新聯繫人被保存到新創建的組中。

// create address book record 
ABAddressBookRef addressBook = ABAddressBookCreate(); 
// create a person 
ABRecordRef person = ABPersonCreate(); 
// name of the new person 

ABRecordSetValue(person, kABPersonFirstNameProperty, [index objectAtIndex:3], nil); 

ABRecordSetValue(person, kABPersonLastNameProperty, [index objectAtIndex:0], nil); 


//add the new person to the record 
ABAddressBookAddRecord(addressBook, person, nil); 

ABAddressBookSave(addressBook, &error); 

ABAddressBookAddRecord(addressBook, group, &error); // add the group 
ABAddressBookSave(addressBook, &error); 
ABRecordRef group = ABGroupCreate(); //create a group   
ABGroupAddMember(group, person, &error); // add the person to the group   
ABAddressBookSave(addressBook, &error); 

//save the record 
ABAddressBookSave(addressBook, nil); 

// relase the ABRecordRef variable 
CFRelease(person); 

這就是我一直在努力的代碼。我真的很感謝所有幫助這是可能的

謝謝

+0

哪裏組變量設置? – theChrisKent 2011-04-20 20:14:07

+0

在將記錄添加到組之前,我更新了我的問題組變量。 – alanvabraham 2011-04-21 01:22:46

回答

1

ABRecordRef group = ABGroupCreate();

這將創建一個新的組...如果你婉在現有的組中添加成員,那麼你應該通過ID獲取組。 更新的代碼

ABRecordRef group = ABAddressBookGetGroupWithRecordID(addressBookInstance,putYourGroupIdHere); 

感謝,

+0

我已經嘗試了你提到的,但我得到一個錯誤,如「初始化使指針沒有投射整數」 – alanvabraham 2011-04-21 06:51:04

+0

抱歉讓你煩惱,但這是我的錯誤。我寫錯了方法,請在上面的答案中看到更新的代碼。謝謝 – Ravin 2011-04-21 07:25:35

+0

@Ravin你能幫我一個問題:[http://stackoverflow.com/questions/10333810/add-contact-to-existing-group-programatically-in-iphone][1] – iOSAppDev 2012-04-27 08:52:09

0
ABAddressBookRef ab = ABAddressBookCreate(); 
CFErrorRef error; 
MySource *source = [sourcesAndGroups objectAtIndex:0]; 
ABRecordRef group = [source.groups objectAtIndex:self.IndexValue]; //Get the Group Name 

NSString *firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 
NSString *lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); 

ABRecordSetValue(group, kABGroupNameProperty,[self nameForGroup: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,lastName, &error); 
isSuccess = ABRecordSetValue(record, kABPersonMiddleNameProperty, firstName , &error); 

ABMultiValueRef phoneNumbers = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty); 
if(ABMultiValueGetCount(phoneNumbers) == 0) 
{ 
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Phone Number" message:@"Please enter Phone number" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [av show]; 
    return; 
} 
CFRelease(phoneNumbers); 
NSString* phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);  

ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonEmailProperty); 
ABMutableMultiValueRef copyOfPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty); 


ABMultiValueAddValueAndLabel(copyOfPhones, phoneNumber,kABPersonPhoneMobileLabel,NULL); 
isSuccess = ABRecordSetValue(record, kABPersonPhoneProperty, copyOfPhones, &error); 
isSuccess = ABRecordSetValue(record, kABPersonEmailProperty, multi, &error); 

isSuccess = ABAddressBookAddRecord(ab, record, &error); 
isSuccess = ABAddressBookSave(ab, &error); 

ABGroupAddMember(group, record, &error); 

NSLog(@"is success %d", isSuccess); 

ABAddressBookSave(ab, &error); 
CFRelease(group); 
相關問題