0

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.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • You say privacy is already set to allow, but are you still going through the motions of actually making the call to the OS to get authorization? I suspect you always have to ask the OS for permission (the OS will only ask the user once). – ghostatron Apr 30 '18 at 21:10
  • no, I'm not making calls to get authorization. I call this piece of code separately to test it. – Teja Nandamuri Apr 30 '18 at 21:26
  • The whole address book framework is pretty much deprecated. You should use `CNContactStore` – Paulw11 Apr 30 '18 at 22:22
  • I totally agree @Paulw11 . We are holding migration to contacts framework because contacts framework don’t provide the contact creation date – Teja Nandamuri Apr 30 '18 at 22:41

0 Answers0