사람들이 iOS의 그룹에 쉽게 추가 및/또는 제거 할 수있는 기능을 앱에서 만들려고합니다. 그러나 내가 추가 및/또는 동시에 첫 번째 조치를 제거하기 위해 여러 사람을 선택하면 한 번 2)에 한 사람을 추가 iOS 주소록 그룹에 여러 사람 추가하기
1)
한 번에을 한 사람을 제거 : 나는 경우 아래 코드는 완벽하게 작동 - 주소록에 추가가 없다면 먼저 추가하거나 제거합니다. 이 방법은 하나의 개별 작업으로 반복해서 호출 할 수 있으며 성공할 수 있습니다. 내가 저장/호출하는 단일 컨텍스트 내에서 여러 추가/삭제를 저장할 수없는 이유를 알 수 없다. 부울이 성공을 결정하는 데 사용 된 코드는 모든 오류가 발생하지 않고 완벽하게 작동한다는 것을 나타냅니다.
로컬 주소록 만들기, groupRef 검색, 각 for 루프 내 주소록 저장 등 다양한 조정 코드 조합을 시도했습니다. 또한 주소록을 소용 없게 저장 한 후 되돌릴 수 있는지 조사했습니다. 생각이나 경험?
-(IBAction)save:(id)sender
{
@synchronized(self)
{
CFErrorRef error = NULL;
BOOL success = NO;
BOOL successfulSave = NO;
ABAddressBookRef localAddressBook = ABAddressBookCreateWithOptions(NULL, &error);
group = ABAddressBookGetGroupWithRecordID(localAddressBook, groupID);
for (CFIndex addItemCount = 0; addItemCount < [theAddList count]; ++addItemCount)
{
NSNumber *addID = [theAddList objectAtIndex:addItemCount];
ABRecordRef thePersonID = ABAddressBookGetPersonWithRecordID(localAddressBook, [addID intValue]);
success = success || ABGroupAddMember(group, thePersonID, &error);
}
for (CFIndex removeItemCount = 0; removeItemCount < [theRemoveList count]; ++removeItemCount)
{
NSNumber *removeID = [theRemoveList objectAtIndex:removeItemCount];
ABRecordRef thePersonID = ABAddressBookGetPersonWithRecordID(localAddressBook, [removeID intValue]);
success = success || ABGroupRemoveMember(group, thePersonID, &error);
}
if (success)
{
successfulSave = ABAddressBookSave(localAddressBook, &error);
}
if (!(successfulSave && success))
{
UIAlertView *addressBookUpdateAlert = [[UIAlertView alloc] initWithTitle:@"AddressBook Error" message:@"An update to your address book could not be processed at this time. Try again shortly." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
// we can't update a GUI element from a background thread - appears sensitivity didn't appear until iOS 8
[[NSOperationQueue mainQueue] addOperationWithBlock:
^{
[addressBookUpdateAlert show];
}];
}
else
{
[[self navigationController] popViewControllerAnimated:YES];
}
}
}