views:

114

answers:

2

I would like to implement the ability to save a contact (phone number, name, address and email) in the iphone address book by clicking on a button in my application! How can I do this?

+1  A: 

Hi Claudio,

Its so easy and follows below steps,

First, you have import following frameworks,

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h

Second, write the following code in your button action and modify the code according to your needs,

        NSString * addressString1 = [appDelegate getCurrentSummary];

        NSString * addressString2 = [appDelegate getCurrentTubeImage];

        NSString * cityName = [appDelegate getCurrentcheckValue];

        NSString * stateName = [appDelegate getCurrentTubeUrl];

        NSString * postal = [appDelegate getCurrentViews];

        NSString * emailString = [appDelegate getCurrentLink];

        NSString * phoneNumber = [appDelegate getCurrentTitle];

        NSString * prefName = [appDelegate getCurrentAuthor];

        ABAddressBookRef libroDirec = ABAddressBookCreate();

        ABRecordRef persona = ABPersonCreate();

        ABRecordSetValue(persona, kABPersonFirstNameProperty, prefName, nil);

            ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

            NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

            NSString *homeStreetAddress=[addressString1 stringByAppendingString:addressString2];

            [addressDictionary setObject:homeStreetAddress forKey:(NSString *) kABPersonAddressStreetKey];

            [addressDictionary setObject:cityName forKey:(NSString *)kABPersonAddressCityKey];

            [addressDictionary setObject:stateName forKey:(NSString *)kABPersonAddressStateKey];

            [addressDictionary setObject:postal forKey:(NSString *)kABPersonAddressZIPKey];

            bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, addressDictionary, kABHomeLabel, NULL);

            if(didAddHome)
            {
                ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL);

                NSLog(@"Address saved.....");
            }

            [addressDictionary release];

        //##############################################################################

        ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);

        bool didAddPhone = ABMultiValueAddValueAndLabel(multiPhone, phoneNumber, kABPersonPhoneMobileLabel, NULL);

        if(didAddPhone){

            ABRecordSetValue(persona, kABPersonPhoneProperty, multiPhone,nil);

            NSLog(@"Phone Number saved......");

        }

        CFRelease(multiPhone);

        //##############################################################################

        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);

        bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, emailString, kABOtherLabel, NULL);

        if(didAddEmail){

        ABRecordSetValue(persona, kABPersonEmailProperty, emailMultiValue, nil); 

        NSLog(@"Email saved......");
        }

        CFRelease(emailMultiValue);

        //##############################################################################

        ABAddressBookAddRecord(libroDirec, persona, nil);

        CFRelease(persona);

        ABAddressBookSave(libroDirec, nil);

        CFRelease(libroDirec);

        NSString * errorString = [NSString stringWithFormat:@"Information are saved into Contact"];

         UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"New Contact Info" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

         [errorAlert show];

        [errorAlert release];

}

I hope it will help you!

Sivanathan
Thank you very much, it work great!!! I wanted ask you just another thing, how can I do to set also the image of the contact???
Claudio
@Claudio follow the link, it has solutionhttp://www.iphonedevsdk.com/forum/iphone-sdk-development/16910-how-add-image-contact.html
Sivanathan
Done...Again thank you very much!! ;-)
Claudio