Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create contacts in address book in iPhone SDK?

How to create contacts in address book in iPhone SDK?

like image 573
meetpd Avatar asked Mar 21 '11 12:03

meetpd


People also ask

Does Apple have an address book?

Apple Contacts (Address Book) is the same why, Contacts is not designed to store only email addresses, its made to store the Full Name, Email, and Phone number of those in the list. You can utilize the iCloud and sync these Contacts with your devices if you choose. New way of thinking.

How do I use Apple Address Book?

In the Contacts app on your Mac, do any of the following: See a contact: In the list of contacts, select a contact. Contact information is shown on the right. See a contact in a separate window: Double-click a contact in the list, or select a contact and choose Card > Open in Separate Window.


2 Answers

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
like image 99
malinois Avatar answered Sep 29 '22 19:09

malinois


-(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

}
like image 26
Avinash Meghanathi Avatar answered Sep 29 '22 18:09

Avinash Meghanathi