alanvabraham
alanvabraham

Reputation: 799

Iphone - Check if a group name already exist in addressbook

Greetings,

I am making an address book application so that contacts can be created under groups. I have been able to create the groups . but everytime i start the application the group is repeated .

How is that i can check if the group already exist. I had used the ABAddressBookCopyArrayOfAllGroups but havent been successful in retrievin the name of the group from that array while printting the NSLog of the array i get an

Upvotes: 1

Views: 1524

Answers (1)

Ashish Chauhan
Ashish Chauhan

Reputation: 1376

+ (BOOL)checkIfGroupExistInAddressBook:(NSString*)gName{

    BOOL gExist = NO;
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *groups = (NSArray *) ABAddressBookCopyArrayOfAllGroups(addressBook);

    // Check group in existing Address book groups
    for (id _group in groups)
    {
        //NSString *currentGroupName = [[[NSString alloc] init] autorelease];
        NSString *currentGroupName = (NSString*) ABRecordCopyValue(_group, kABGroupNameProperty);
        //(ABRecordRef)group]
        // If group exist return YES
        if ([currentGroupName isEqualToString:gName]){
            gExist = YES;
            CFRelease(currentGroupName);
            break;
        }

        CFRelease(currentGroupName);
    }

    CFRelease(addressBook);
    if (groups) {[groups release];groups = nil;}

    // If group Dose not exist return NO;
    return gExist;

}

Upvotes: 6

Related Questions