Creating/Deleting an addressbook group fails in iPad device

32 Views Asked by At

I'm trying to manage the contacts of my app by creating a group.

Creating/Deleting a group works fine on simulator (with iOS 11.3 and with iOS 9.3).

In both cases, the default contact settings are set to iCloud in the iOS settings.

I have checked privacy settings in both cases, the permission to access the device contacts was set to YES

The following code fails on iPad with iOS 9.3.5.

-(void) createNewGroup:(NSString*)groupName
{
    CFErrorRef error = NULL;
    ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, &error);
    if (ab) {
        ABRecordRef newGroup = ABGroupCreate();
        ABRecordSetValue(newGroup, kABGroupNameProperty,(__bridge CFTypeRef)(groupName), nil);
        ABAddressBookAddRecord(ab, newGroup, &error);

        if (error) {
             DDLogDebug(@"cannot create group -%@",error);
        }else{
            ABAddressBookSave(ab, &error);
        }

        if (error) {
           DDLogDebug(@"cannot save group -%@",error);
        }
        //!!! important - save groupID for later use
        self.groupId = ABRecordGetRecordID(newGroup);  //-1 for iPad, and for other cases 2,3,4 ....
        DDLogDebug(@"createNewGroup : %i",self.groupId );

        CFRelease(newGroup);
        CFRelease(ab);
    }
}

and to delete the group:

Note: I did not test this method on iPad as there is no group that I want to delete, I will test this after I'm able to create a group on the iPad. This method worked as expected on simulator.

-(BOOL)deleteGroup:(NSString *)groupName{

    BOOL res;
    CFErrorRef error;
    ABAddressBookRef ab = ABAddressBookCreate();

    NSArray *groups = (__bridge NSArray *) ABAddressBookCopyArrayOfAllGroups(ab);

    for (id _group in groups)
    {
        NSString *currentGroupName = (__bridge NSString*) ABRecordCopyValue((__bridge ABRecordRef)(_group), kABGroupNameProperty);
        DDLogDebug(@"group name -%@",currentGroupName);
        if ([groupName isEqualToString:currentGroupName])
        {
            res = ABAddressBookRemoveRecord(ab, (__bridge ABRecordRef)(_group), &error);
            return res;
        }

    }

    return NO;
}

I do not get any error but I get the groupID as -1 when trying to create group on iPad, and I check that group isn't created at all.

0

There are 0 best solutions below