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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With