2012-11-13 18 views
2

我需要防止在我們的應用程序中顯示聯繫人組,但是它侵入用戶從地址簿中刪除組。因此,我試圖在顯示聯繫人之前將其刪除,然後在完成後將其添加回來,以便AddressBook保持不變,iOS Contacts應用程序按原樣顯示這些組。我創建了兩個陣列存儲的信息:爲什麼ABGroupAddMember失敗?它返回NO並且不設置CFErrorRef值

NSArray *aGroups; 
NSMutableArray *aGroupMembers; 

我刪除了組,存儲它們,並顯示選擇器在viewWillAppear中:

// Remove group records for the life of this view. 
CFErrorRef error; 
ABAddressBookRef abRef = ABAddressBookCreate(); 
NSArray *groups = (NSArray *)ABAddressBookCopyArrayOfAllGroups(abRef); 
if (groups.count > 0) { 

    // we will remove the groups so save for restoration 
    self.aGroups = [[NSArray alloc] initWithArray:groups]; 
    aGroupMembers = [[NSMutableArray alloc] initWithCapacity:groups.count]; 
    for (int i = 0; i < groups.count; i++) { 

     NSArray *members = (NSArray *)ABGroupCopyArrayOfAllMembers([groups objectAtIndex:i]); 
     NSMutableArray *memberIDs = [[NSMutableArray alloc] initWithCapacity:[members count]]; 
     for (id member in members) { 
      ABRecordID ID = ABRecordGetRecordID(member); 
      [memberIDs addObject:[NSNumber numberWithInteger:ID]]; 
     } 

     [aGroupMembers insertObject:memberIDs atIndex:i]; 
     CFRelease(members); 
     [memberIDs release]; 

     // Remove the group from the addressbook 
     ABAddressBookRemoveRecord(abRef, [groups objectAtIndex:i], &error); 
    } 

    CFRelease(groups); 
    ABAddressBookSave(abRef, nil); 
    self.picker.addressBook = abRef; 
} 

CFRelease(abRef); 

而且後來兩個viewWillDisappear和willResignActive我「嘗試」重新創建組,重新添加成員到每個組並添加回地址簿。但是,我無法將成員添加回去,因爲ABGroupAddMember()失敗。當我在調試器中檢查變量時,GroupName和person的名字都是正確的。我看不到問題,並且CFErrorRef值沒有設置。

// Restore the group records. 
if (self.aGroups != nil) { 

    CFErrorRef error = nil; 
    CFTypeRef grpName = nil; 
    CFTypeRef firstName = nil; 
    ABRecordRef newGroup = nil; 
    ABAddressBookRef abRef = ABAddressBookCreate(); 

    // Re-create the groups 
    for (int i = 0; i < self.aGroups.count; i++) { 

     // Create the new group 
     newGroup = ABGroupCreate(); 
     grpName = ABRecordCopyValue((ABRecordRef)[self.aGroups objectAtIndex:i], kABGroupNameProperty); 
     ABRecordSetValue(newGroup, kABGroupNameProperty, grpName, &error); 

     // Create the members 
     NSArray *memberIDs = (NSArray*)[aGroupMembers objectAtIndex:i]; 
     for (NSNumber *iD in memberIDs) { 

      ABRecordRef person = ABAddressBookGetPersonWithRecordID(abRef,iD.intValue); 
      firstName = ABRecordCopyValue((ABRecordRef)person, kABPersonFirstNameProperty);     
      BOOL bSuccess = ABGroupAddMember(newGroup, person, &error); 
      if (!bSuccess) { 
       //NSString *errorStr = [(NSString *)CFErrorCopyDescription(error) autorelease]; // this cause EXEC_BAD_ACCESS 
       CFRelease(error); 
      } 
     } 

     CFRelease(newGroup); 
     CFRelease(grpName); 
    } 

    // Save the changes 
    ABAddressBookSave(abRef, nil); 

    CFRelease(abRef); 
    self.aGroups = nil; 
    [aGroupMembers removeAllObjects]; 
    aGroupMembers = nil; 
} 
+0

http://stackoverflow.com/questions/15470733/unable-to-add-contacts-into-specific-group-in-iphone-using-abgroupaddmember/23862110#23862110 – Yong

+0

http://stackoverflow.com/a/31998913/2359049 –

回答

1

您可能需要嘗試成員添加到組ABGroupAddMember前羣創與ABAddressBookAddRecord添加到地址簿。

+0

我與模擬器有同樣的問題:爲了將聯繫人添加到新組,我必須先將空組添加到地址簿,然後保存並重新打開地址簿,並且僅然後將聯繫人添加到新的新組。當我在將聯繫人添加到新的新組之前沒有保存地址簿時,它不起作用。 –

2

我試圖通過CardDAV協議將同步到我的iPhone的聯繫人添加到以編程方式創建的本地組,從而發生了這種情況。

事實證明,您不能將同步的聯繫人添加到本地羣組,反之亦然,但它只是返回NO而不將error參數設置爲任何錯誤消息,以解釋其無效的原因。

我在我的應用程序中通過嘗試將聯繫人添加到組中解決了它,如果它不起作用,我通過在本地應用程序中本地保存聯繫人組關係來解決此問題。

不是很好,如果有人有辦法解決這個問題,我會很高興聽到。

相關問題