2011-04-19 71 views
4

問候,在地址簿中創建一個組用於iphone

我正在使用添加聯繫人到地址簿的iphone應用程序。我已經能夠將聯繫人添加到地址簿中,但是我面臨的問題是將聯繫人記錄添加到我創建的組中。該聯繫人創建於所有不在創建的組內的聯繫人。 下面是我用

// create address book record 
ABAddressBookRef addressBook = ABAddressBookCreate(); 
// create a person 
ABRecordRef person = ABPersonCreate(); 
// first name of the new person 
ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil); 
// his last name 
ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil); 
//add the new person to the record 
ABAddressBookAddRecord(addressBook, person, nil); 

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 

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

// relase the ABRecordRef variable 
CFRelease(person); 

的代碼,我將不勝感激這方面

感謝提供任何幫助!

+0

你的意思是你不能將人添加到組中嗎? – dks1725 2011-04-19 07:04:18

+0

是這個人沒有被添加到該組,但它被添加到所有聯繫人。 – alanvabraham 2011-04-19 07:28:04

回答

0

首先,您需要保存個人地址簿將其添加到組之前,這意味着你必須添加一個

ABAddressBookSave(addressBook, nil); 

添加的人到組之前,在你的情況下,將就在創建組之前。

+0

我已經嘗試了,因爲您已經提到過但仍然無效聯繫人保存在組內的所有聯繫人 – alanvabraham 2011-04-19 10:17:26

+1

至於地址簿中的人員,您還需要在添加組後保存,然後再添加人到組,所有的東西都需要保存在地址簿中。所以創建人,保存。創建組,保存。將該人添加到組中,保存。 – StrAbZ 2011-04-19 12:16:06

+0

我試過它也喜歡,它仍然沒有工作。你知道任何其他方式在地址簿中添加聯繫人 – alanvabraham 2011-04-19 13:10:25

4

這是我的測試,我測試了一下,效果很好。

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); 
+0

ThankYou它工作:) – alanvabraham 2011-05-10 05:05:39

+0

上述代碼的問題是,將創建名爲「我的組」的多個組。它不會將內容添加到現有組,這是原始問題的要求。而我卡住的地方:-) – firecall 2012-05-10 03:33:21