Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a contact from my app to the iOS address book

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

like image 634
Claudio Avatar asked Jul 26 '10 10:07

Claudio


1 Answers

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!

like image 188
Sivanathan Avatar answered Sep 25 '22 02:09

Sivanathan