Here's what I'm doing:
- (void)doCreateGroup {
[[self contentView] endEditing:true];
NSString * newString = [[[[self contentView] groupNameField] text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * firstError = nil;
if ([newString length] == 0) {
firstError = @"Missing group name";
}
NSError * groupsError = nil;
NSArray * groups = [self.contactStore groupsMatchingPredicate:nil error:&groupsError];
for (CNGroup * group in groups) {
if ([group.name isEqualToString:newString]) {
firstError = @"Group already exists";
}
}
if (firstError) {
[self presentViewController:[WLGCommonUtilities doProcessErrorWithOkay:@"Error" errorMessage:firstError] animated:YES completion:nil];
return;
}
CNMutableGroup * newGroup = [CNMutableGroup new];
[newGroup setName:newString];
CNSaveRequest *saveRequest = [CNSaveRequest new];
[saveRequest addGroup:newGroup toContainerWithIdentifier:nil];
NSError * error = nil;
[self.contactStore executeSaveRequest:saveRequest error:&error];
if (error) {
[self presentViewController:[WLGCommonUtilities doProcessErrorWithOkay:@"Error" errorMessage:[error localizedDescription]] animated:YES completion:nil];
} else {
CNSaveRequest *saveRequest2 = [CNSaveRequest new];
NSArray * groupsAgain = [self.contactStore groupsMatchingPredicate:nil error:&groupsError];
CNGroup * gotGroup;
for (CNGroup * group in groupsAgain) {
if ([group.name isEqualToString:newString]) {
gotGroup = group;
}
}
for (CNContact * contact in self.selectedContactsArray) {
[saveRequest2 addMember:contact toGroup:gotGroup];
}
NSError * error1 = nil;
[self.contactStore executeSaveRequest:saveRequest2 error:&error1];
if (error) {
[self presentViewController:[WLGCommonUtilities doProcessErrorWithOkay:@"Error" errorMessage:[error1 localizedDescription]] animated:YES completion:nil];
} else {
[[self navigationController] dismissViewControllerAnimated:true completion:nil];
}
}
}
this works to create a CNGroup and then add contacts to said CNGroup. Works for all contacts EXCEPT for unified contacts. I've tried everything possible to make this work and it just doesn't. It likely has something to do with the unified CNContact's identifier since that identifier is only stored in temp memory so it can't be added to a CNGroup since it doesn't really haver a REAL CNContact identifier. Contacts framework is a mess! Any help would be appreciated. I've also filed a tech support request with Apple.
EDIT: One way to get around this is to use Address Framework that is now deprecated. I can add as many unified contacts to Address groups by doing this.
ABRecordRef group = ABGroupCreate();
ABAddressBookAddRecord(addressBook, group, nil);
ABRecordSetValue(group, kABGroupNameProperty,@"My Groups", nil);
for (int i=0;i < nPeople;i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
ABGroupAddMember(group, ref, nil);
ABAddressBookSave(addressBook, nil);
}
this does save everything in the contact book to a group, all visible contacts that is. so it does store the Unified contact into the group. if you unlink the contacts while they are in a group, both contacts stay within the group. so the old framework works to solve this. just seems ridiculous that it can't be solved with new Contacts framework. Again, I may be missing something with the new Contacts framework, so if this is possible with the current Contacts framework in iOS please let me know.
from Can't add unified CNContact to CNGroup in iOS
No comments:
Post a Comment