2013-03-18 63 views
1

我試圖將選定的聯繫人添加到特定的組,但聯繫人不能添加到組中。 我想要創建新組並將聯繫人添加到該組中。但該組無法添加contacts.here創建組後是我的代碼創建新組並添加聯繫人到組:無法使用ABGroupAddMember將聯繫人添加到iPhone中的特定組中

addressBook = ABAddressBookCreateWithOptions(nil, &err); 
    group = ABGroupCreate(); 
    ABRecordSetValue(group, kABGroupNameProperty, txtGroupNameText.text, &err); 
    ABAddressBookAddRecord(addressBook, group, &err); 
    ABAddressBookSave(addressBook, &err); 
    groupId = ABRecordGetRecordID(group); 

和添加聯繫人:

 ABRecordRef person = [contactArray objectAtIndex:i]; 
     ABRecordRef HiByeGroup = ABAddressBookGetGroupWithRecordID(addressBook,  self.groupId); 
     BOOL didAdd = ABGroupAddMember(HiByeGroup,person,&err); 

     if (didAdd) { 
      // Update to handle the error appropriately. 
      NSLog(@"Unresolved error while adding person to HiBye group"); 
     } 

     BOOL didSave = ABAddressBookSave(addressBook, &err); 

     if (didSave) { 
      // Update to handle the error appropriately. 
      NSLog(@"Unresolved error while saving address book"); 
     } 

電子觸點節約但不得到add.Please幫助我,謝謝。

回答

3

首先,您需要保存個人地址簿將其添加到組之前。

例如試試這個代碼,它工作得很好

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

謝謝,它的作品不錯,但我的所有聯繫人的一個可變數組並從該數組我試圖添加到組接觸,我怎樣才能做到這一點?因爲我的應用程序崩潰了ABRecordRef person = [contactArray objectAtIndex:i];在這條線上。 – newDev

1
ABAddressBookRef _addressBook; 
CFErrorRef error; 
_addressBook = ABAddressBookCreateWithOptions(NULL, &error); 

ABRecordRef _source; 
CFArrayRef sourceArray = ABAddressBookCopyArrayOfAllSources(_addressBook); 
if (sourceArray != NULL) { 
    CFIndex count = CFArrayGetCount(sourceArray); 
    CFIndex i=0; 
    BOOL foundLocalSource = FALSE; 
    while ((i<count) && (!foundLocalSource)) { 
     ABRecordRef record = CFArrayGetValueAtIndex(sourceArray, i++); 

     // determine source type 
     CFNumberRef sourceTypeNum = (CFNumberRef)ABRecordCopyValue(record, kABSourceTypeProperty); 
     ABSourceType sourceType; 
     CFNumberType numType = CFNumberGetType(sourceTypeNum); 
     CFNumberGetValue(sourceTypeNum, numType, &sourceType); 
     CFRelease(sourceTypeNum); 
     ABSourceType cleanSsourceType = (sourceType & (~kABSourceTypeSearchableMask)); 
     if (cleanSsourceType == kABSourceTypeLocal) { 
      foundLocalSource = true; 
      ABRecordID recordId = ABRecordGetRecordID(record); 
      _source = ABAddressBookGetSourceWithRecordID(_addressBook, recordId); 
     } 
    } 
    CFRelease(sourceArray); 
} 

// Create Group 
ABRecordRef _group; 
_group = ABGroupCreateInSource(_source); 
ABRecordSetValue (_group, kABGroupNameProperty, CFSTR("Yong's Group"), &error); 
//save group record 
if (ABAddressBookAddRecord (_addressBook, _group, &error)) { 
    NSLog(@"Accounts group successfully added to addressbook!"); 
} 

if (ABAddressBookSave(_addressBook, &error)) { 
    NSLog(@"Address Book Saved successfully!"); 
} 
// create Person In Group 
ABRecordRef result = ABPersonCreateInSource(_source); 
ABRecordSetValue(result, kABPersonLastNameProperty,(CFTypeRef)@"yong03", &error); 

CFErrorRef errorRef; 

if (ABGroupAddMember(_group, result, &errorRef)) { 
    NSLog(@"ABGroupAddMember successfully!"); 
} 

if (ABAddressBookAddRecord(_addressBook, result, &errorRef)) { 
    NSLog(@"ABAddressBookAddRecord successfully!"); 
} 

if (ABAddressBookSave(_addressBook, &errorRef)) { 
    NSLog(@"ABAddressBookSave successfully!"); 
} 
相關問題