How to create contacts in address book in iPhone SDK?
Asked Answered
P

2

13

How to create contacts in address book in iPhone SDK?

Prat answered 21/3, 2011 at 12:16 Comment(0)
S
27
ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
ABRecordRef person = ABPersonCreate(); // create a person

NSString *phone = @"0123456789"; // the phone number to add

//Phone number is a list of phone number, so create a multivalue    
ABMutableMultiValueRef phoneNumberMultiValue  = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(person, kABPersonFirstNameProperty, @"FirstName" , nil); // first name of the new person
ABRecordSetValue(person, kABPersonLastNameProperty, @"LastName", nil); // his last name
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, &anError); // set the phone number property
ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

ABRecordRef group = ABGroupCreate(); //create a group
ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); // set group's name
ABGroupAddMember(group, person, &error); // add the person to the group
ABAddressBookAddRecord(addressBook, group, &error); // add the group


ABAddressBookSave(addressBook, nil); //save the record



CFRelease(person); // relase the ABRecordRef  variable
Sejant answered 21/3, 2011 at 13:43 Comment(8)
thanks its working but can u please explain coding.... and how can i make group ??Sardis
I have added some comments, and code to create groups. It's OK ?Dig
hey code is working great and even making group but its not adding any contact !!!! :(Sardis
Its not adding an contact in group !!! its only creating group and showing contact in address book !!! Main thing is NOT ADDING IN GROUPSardis
Try without ABAddressBookAddRecord(addressBook, person, nil);Dig
Still its not adding contacts in group !!Sardis
its working great... thanks...... but can you please help that how can i check that whether the group is available or not ??Sardis
@malinois Is there any API using which one can create the Groups & listdown them in iPhone ?Oxysalt
R
6
-(void)addContactInContactBook:(Info *)objInfo:(BOOL)isInsert:(BOOL)isUpdate:(BOOL)isDelete
{

if (isInsert)
{
    ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
    ABRecordRef person = ABPersonCreate(); // create a person

    CFErrorRef  anError = NULL;
    ABMutableMultiValueRef phoneNumberMultiValue =
    ABMultiValueCreateMutable(kABPersonPhoneProperty);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.cellPhone),kABPersonPhoneMobileLabel, NULL);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.phone),kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil); // set the phone number property


    // Address      
    ABMutableMultiValueRef address =
    ABMultiValueCreateMutable(kABDictionaryPropertyType);
    // Set up keys and values for the dictionary.
    CFStringRef keys[5];
    CFStringRef values[5];
    keys[0] = kABPersonAddressStreetKey;
    keys[1] = kABPersonAddressCityKey;
    keys[2] = kABPersonAddressStateKey;
    keys[3] = kABPersonAddressZIPKey;
    keys[4] = kABPersonAddressCountryKey;

    CFStringRef ref1 = (__bridge_retained CFStringRef)objInfo.street;

    CFStringRef ref2 = (__bridge_retained CFStringRef)objInfo.city;

    CFStringRef ref3 = (__bridge_retained CFStringRef)objInfo.state;

    CFStringRef ref4 = (__bridge_retained CFStringRef)objInfo.zipCode;

    CFStringRef ref5 = (__bridge_retained CFStringRef)objInfo.country;

    values[0] = ref1;
    values[1] = ref2;
    values[2] = ref3;
    values[3] = ref4;
    values[4] = ref5;



    CFDictionaryRef dicref = CFDictionaryCreate(kCFAllocatorDefault, (void *)keys, (void *)values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    ABMultiValueIdentifier identifier;
    ABMultiValueAddValueAndLabel(address, dicref, kABHomeLabel, &identifier);
    ABRecordSetValue(person, kABPersonAddressProperty, address,&anError);


    if (![objInfo.FName isEqualToString:@""])
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)objInfo.FName , nil); // first name of the new person
    if (![objInfo.LName isEqualToString:@""])
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(objInfo.LName), nil); // his last name
    //    ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFTypeRef)(txtEmail.text), nil);

    if (![objInfo.Email isEqualToString:@""])
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(objInfo.Email), (CFStringRef)@"Global", NULL);
        ABRecordSetValue(person, kABPersonURLProperty, emailMultiValue, nil);
    }
    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(objInfo.company), nil);
    //    CFRelease(emailMultiValue);

    ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(objInfo.note), nil);


    ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

    //    ABRecordRef group = ABGroupCreate(); //create a group
    //    ABRecordSetValue(group, kABGroupNameProperty,@"My Group", nil); // set group's name
    //    ABGroupAddMember(group, person, nil); // add the person to the group
    //    ABAddressBookAddRecord(addressBook, group, nil); // add the group


    BOOL isSaved = ABAddressBookSave(addressBook, &anError); //save the record

    if (isSaved) 
    {
        NSInteger num = ABRecordGetRecordID(person);

        NSString *qry = [NSString stringWithFormat:@"update %@ set phonebookId=%d,isSyncPhonebook='TRUE' where id = %d",TABLE,num,objInfo.infoId];
        [appDel insertORUpdateInfo:qry];
    }

}
else if(isUpdate)
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, objInfo.phonebookId);
    if(person != NULL)
    {
        CFErrorRef  anError = NULL;
        ABMutableMultiValueRef phoneNumberMultiValue =
        ABMultiValueCreateMutable(kABPersonPhoneProperty);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.cellPhone),kABPersonPhoneMobileLabel, NULL);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)(objInfo.phone),kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil); // set the phone number property


        // Address      
        ABMutableMultiValueRef address =
        ABMultiValueCreateMutable(kABDictionaryPropertyType);
        // Set up keys and values for the dictionary.
        CFStringRef keys[5];
        CFStringRef values[5];
        keys[0] = kABPersonAddressStreetKey;
        keys[1] = kABPersonAddressCityKey;
        keys[2] = kABPersonAddressStateKey;
        keys[3] = kABPersonAddressZIPKey;
        keys[4] = kABPersonAddressCountryKey;

        CFStringRef ref1 = (__bridge_retained CFStringRef)objInfo.street;

        CFStringRef ref2 = (__bridge_retained CFStringRef)objInfo.city;

        CFStringRef ref3 = (__bridge_retained CFStringRef)objInfo.state;

        CFStringRef ref4 = (__bridge_retained CFStringRef)objInfo.zipCode;

        CFStringRef ref5 = (__bridge_retained CFStringRef)objInfo.country;

        values[0] = ref1;
        values[1] = ref2;
        values[2] = ref3;
        values[3] = ref4;
        values[4] = ref5;



        CFDictionaryRef dicref = CFDictionaryCreate(kCFAllocatorDefault, (void *)keys, (void *)values, 5, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

        ABMultiValueIdentifier identifier;
        ABMultiValueAddValueAndLabel(address, dicref, kABHomeLabel, &identifier);
        ABRecordSetValue(person, kABPersonAddressProperty, address,&anError);


        if (![objInfo.FName isEqualToString:@""])
            ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)objInfo.FName , nil); // first name of the new person
        if (![objInfo.LName isEqualToString:@""])
            ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(objInfo.LName), nil); // his last name
        //    ABRecordSetValue(person, kABPersonEmailProperty, (__bridge CFTypeRef)(txtEmail.text), nil);

        if (![objInfo.Email isEqualToString:@""])
        {
            ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(objInfo.Email), (CFStringRef)@"Global", NULL);
            ABRecordSetValue(person, kABPersonURLProperty, emailMultiValue, nil);
        }
        ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(objInfo.company), nil);
        //    CFRelease(emailMultiValue);

        ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(objInfo.note), nil);


        //        ABAddressBookAddRecord(addressBook, person, nil); //add the new person to the record

        //    ABRecordRef group = ABGroupCreate(); //create a group
        //    ABRecordSetValue(group, kABGroupNameProperty,@"My Group", nil); // set group's name
        //    ABGroupAddMember(group, person, nil); // add the person to the group
        //    ABAddressBookAddRecord(addressBook, group, nil); // add the group


        BOOL isSaved = ABAddressBookSave(addressBook, &anError); //save the record

        if (isSaved) 
        {
//                NSInteger num = ABRecordGetRecordID(person);

            NSString *qry = [NSString stringWithFormat:@"update %@ set isUpdatePhonebook='TRUE' where id = %d",TABLE,objInfo.infoId];
            [appDel insertORUpdateInfo:qry];
        }

    }
//        ABAddressBookRef addressBook = ABAddressBookCreate(); // create address book record
//        ABRecordRef person = ABPersonCreate(); // create a person


}
else if(isDelete)
{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, objInfo.phonebookId);
    if(person != NULL)
    {
        CFErrorRef  anError = NULL;
        ABAddressBookRemoveRecord(addressBook, person, &anError);
        BOOL isSaved = ABAddressBookSave(addressBook, &anError); //save the record

        if (isSaved) 
        {
//                NSInteger num = ABRecordGetRecordID(person);

            NSString *qry = [NSString stringWithFormat:@"update %@ set isDeletePhonebook='TRUE' where id = %d",TABLE,objInfo.infoId];
            [appDel insertORUpdateInfo:qry];
        }

    }
}

//    CFRelease(person); // relase the ABRecordRef  variable

}
Riba answered 12/12, 2012 at 4:46 Comment(1)
does this work if user has multiple home or work addressess..?? I have a problem saving multiple home or multiple work addresses.Used

© 2022 - 2024 — McMap. All rights reserved.