0

I'm doing and App in IOS and I want to add a Button that when people clik it it automatically add the contact (name, number and Picture) in the phone contact list

the contact name is Clinica Lo Curro and phone No. is 6003667800

Someone can please help me with the code please?

regards,

Eugenio Durán

1 Answers1

0

Add AddressBook.framework and AddressBookUI.framework from Build Phase,Link Binary with Libraries. And Import AddressBookUI to your header File as displayed below.

#import <AddressBookUI/AddressBookUI.h>

Include delegate ABPeoplePickerNavigationControllerDelegate to header file.

and Call The following method to add Contact.

-(IBAction)addContact:(id)sender
{
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    UIImage *personImage = [UIImage imageNamed:@"cinema.png"];
    NSData *dataRef = UIImagePNGRepresentation(personImage);

    NSString *firstName=@"AKASH";
    NSString *lastName=@"MALHOTRA";

    NSString *organization=@"Aua Comp Pvt Ltd.";
    NSString *jobTitle=@"iPhone App Developer";
    NSString *departMent=@"Mobile Division";
    NSString *webURL=@"http://www.google.com";
    NSString *personEmail=@"goel.anjan@gmail.com";
    NSString *phoneNo=@"9856756445 or 7656876765 or 8976566775";
    NSString *personNote=@"I am just a kid";

    NSString *addressOne=@"HN-23,Sector-2,Chandigarh";
    NSString *addressTwo=@"AL-19,Sector-5,SaltLake";

    NSString *cityName=@"Kolkata";
    NSString *stateName=@"West Bengal";
    NSString *pinCode=@"700091";
    NSString *country=@"India";

    CFErrorRef cfError=nil;


    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (firstName) {
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
    }

    if (lastName) {
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName) , nil);
    }

    if (jobTitle) {
        ABRecordSetValue(person, kABPersonJobTitleProperty,(__bridge CFTypeRef)(jobTitle), nil);
    }

    if (departMent) {
        ABRecordSetValue(person, kABPersonDepartmentProperty,(__bridge CFTypeRef)(departMent), nil);
    }

    if (personNote) {
        ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(personNote), nil);
    }

    if (dataRef) {
        ABPersonSetImageData(person, (__bridge CFDataRef)dataRef,&cfError);
    }


    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
    CFRelease(multiAddress);


    //Add person Object to addressbook Object.
    ABAddressBookAddRecord(addressBook, person, &cfError);

    if (ABAddressBookSave(addressBook, nil)) {
        NSLog(@"\nPerson Saved successfuly");
    } else {
        NSLog(@"\n Error Saving person to AddressBook");
    }
}
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90