Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Add contact to address book

sindhutiwarisindhutiwari Posts: 547Registered Users
hello friends,

My aim is to add a contact to address book of iphone ,

Can this be done ?

if yes then a piece of code or a hint , will be good at this point

Thanks and Regards
Post edited by sindhutiwari on
In order to succeed, your desire for success has to be greater than your fear for failure

Replies

  • sindhutiwarisindhutiwari Posts: 547Registered Users
    This is the Code which i have got .. but when i looked back into my address book on simulator it was not there

    Code is
    [HTML]
    fName = @"some Text";
    lName = @"some Text";
    phone = @"0123456789";

    ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,phone,kABPersonPhoneMobileLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty,lName,&anError);
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,fName,&anError);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneNumberMultiValue, &anError);
    ABAddressBookAddRecord(addressBook, newPerson, &anError);
    ABAddressBookSave(addressBook, &anError)[/HTML]
    In order to succeed, your desire for success has to be greater than your fear for failure
  • david_daviddavid_david Posts: 372Registered Users
    this is the working example

    ABRecordRef aRecord = ABPersonCreate(); 
    CFErrorRef anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
    CFSTR(\"Jijo\"), &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty,
    CFSTR(\"Pulikkottil\"), &anError);
    if (anError != NULL) {

    NSLog(@\"error while creating..\");
    }
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    lastName = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);




    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    BOOL isAdded = ABAddressBookAddRecord (
    addressBook,
    aRecord,
    &error
    );

    if(isAdded){

    NSLog(@\"added..\");
    }
    if (error != NULL) {
    NSLog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;

    BOOL isSaved = ABAddressBookSave (
    addressBook,
    &error
    );

    if(isSaved){

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

    if (error != NULL) {
    NSLog(@\"ABAddressBookSave %@\", error);
    }

    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);
    CFRelease(addressBook);



    don't forget add AddressBook.Framework
    Ref: AddressBookProgrammingGuideforiPhone.pdf
    lazy blogs

    The Lord's holy name be praised.
  • AmitSrivastavaAmitSrivastava Posts: 79Registered Users
    david_david;65925 said:
    this is the working example

    ABRecordRef aRecord = ABPersonCreate(); 
    CFErrorRef anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
    CFSTR(\"Jijo\"), &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty,
    CFSTR(\"Pulikkottil\"), &anError);
    if (anError != NULL) {

    NSLog(@\"error while creating..\");
    }
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    lastName = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);




    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    BOOL isAdded = ABAddressBookAddRecord (
    addressBook,
    aRecord,
    &error
    );

    if(isAdded){

    NSLog(@\"added..\");
    }
    if (error != NULL) {
    NSLog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;

    BOOL isSaved = ABAddressBookSave (
    addressBook,
    &error
    );

    if(isSaved){

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

    if (error != NULL) {
    NSLog(@\"ABAddressBookSave %@\", error);
    }

    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);
    CFRelease(addressBook);



    don't forget add AddressBook.Framework
    Ref: AddressBookProgrammingGuideforiPhone.pdf

    Thanks for above code,
    but i am still not able to add contact in iphone address book database.
    i have write above code in a function which is called by rightBarbutton,
    i have three textField in page which value i have to add in address Book database.

    can you plz help me where and how to write the code, or if possible mail me the project folder at [email]smarti_vns@yahoo.co.in[/email]
  • david_daviddavid_david Posts: 372Registered Users
  • AmitSrivastavaAmitSrivastava Posts: 79Registered Users
    david_david;83120 said:
    and no error ?
    many error...
    m working that..
  • AmitSrivastavaAmitSrivastava Posts: 79Registered Users
    david_david;83120 said:
    and no error ?
    hey its working,
    thanks a lot....
    now i have to add contacts details after 5 pages,
    so i have to fetch all the details of 4 page at 5th page.
    can you give me any suggestion how to fetch that values in 5th page.

    once again thank u very much.
  • david_daviddavid_david Posts: 372Registered Users
    what do you mean by 'pages' "contacts details after 5 pages,"
    lazy blogs

    The Lord's holy name be praised.
  • AmitSrivastavaAmitSrivastava Posts: 79Registered Users
    david_david;83139 said:
    what do you mean by 'pages' "contacts details after 5 pages,"
    i have to create a contact wizard, which consist the following details
    1st page= 3 text field (first name, last name, middle name)
    2nd page= 1 picker view+ 1 text field
    and 2 more pages
    and at the last 5th page, there is a save button on which i have to write a function to save all the details in address book database.

    means i have to save the value of all pages and fetch those in 5th page.

    or simply can say "code for fetch value of a TextField of view1 into a NSString of view2".
  • david_daviddavid_david Posts: 372Registered Users
    AmitSrivastava;83156 said:
    i have to create a contact wizard, which consist the following details
    1st page= 3 text field (first name, last name, middle name)
    2nd page= 1 picker view+ 1 text field
    and 2 more pages
    and at the last 5th page, there is a save button on which i have to write a function to save all the details in address book database.

    means i have to save the value of all pages and fetch those in 5th page.

    or simply can say "code for fetch value of a TextField of view1 into a NSString of view2".

    It would be better start new thread for new topic
    lazy blogs

    The Lord's holy name be praised.
  • chaitanyakvchaitanyakv Posts: 1New Users
    Hello Amit Srivastava,
    I am stuck with same problem can you please send me the code of source code to add contact in address book database.


    [email]Chaitanyakv@live.com[/email]
    Thanks in advance.
  • vikysaranvikysaran Posts: 130Registered Users
    david_david;65925 said:
    this is the working example

    ABRecordRef aRecord = ABPersonCreate(); 
    CFErrorRef anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
    CFSTR(\"Jijo\"), &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty,
    CFSTR(\"Pulikkottil\"), &anError);
    if (anError != NULL) {

    NSLog(@\"error while creating..\");
    }
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    lastName = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);




    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    BOOL isAdded = ABAddressBookAddRecord (
    addressBook,
    aRecord,
    &error
    );

    if(isAdded){

    NSLog(@\"added..\");
    }
    if (error != NULL) {
    NSLog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;

    BOOL isSaved = ABAddressBookSave (
    addressBook,
    &error
    );

    if(isSaved){

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

    if (error != NULL) {
    NSLog(@\"ABAddressBookSave %@\", error);
    }

    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);
    CFRelease(addressBook);



    don't forget add AddressBook.Framework
    Ref: AddressBookProgrammingGuideforiPhone.pdf
    working great.
    I added phone number, website, email etc also and its saving everything :)
    Thanx & Regards-

    Vaibhav Saran | Software Engineer

    Neo Sypher Systems Pvt. Ltd.

    New Delhi | India

    Websites:

    <font color="
  • ddotddot Posts: 3New Users
    vikysaran;213691 said:
    working great.
    I added phone number, website, email etc also and its saving everything :)
    hey everyone, i know this thread is old, but i am having the same problem , and been trying to figure it out for 2 days, can you pls send me the project or post the code if u still have it. i can paste in what i have as well instead, maybe someone can help me with debugging that.

    thx
  • vikysaranvikysaran Posts: 130Registered Users
    i made the code below accordingly i needed. Check this out, it might help you
    ABRecordRef aRecord = ABPersonCreate(); 
    CFErrorRef anError = NULL;

    // Username
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, username, &anError);

    // Phone Number.
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multi, (CFStringRef)usercontact, kABWorkLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
    CFRelease(multi);

    // Company
    ABRecordSetValue(aRecord, kABPersonDepartmentProperty, usercompany, &anError);

    // email
    NSLog(useremail);
    ABMutableMultiValueRef multiemail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiemail, (CFStringRef)useremail, kABWorkLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonEmailProperty, multiemail, &anError);
    CFRelease(multiemail);

    // website
    NSLog(userwebsite);
    ABMutableMultiValueRef multiweb = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiweb, (CFStringRef)userwebsite, kABWorkLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonURLProperty, multiweb, &anError);
    CFRelease(multiemail);

    // Function
    ABRecordSetValue(aRecord, kABPersonJobTitleProperty, userrole, &anError);

    if (anError != NULL)
    NSLog(@\"error while creating..\");

    CFStringRef personname, personcompind, personemail, personfunction, personwebsite, personcontact;

    personname = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    personcompind = ABRecordCopyValue(aRecord, kABPersonDepartmentProperty);
    personfunction = ABRecordCopyValue(aRecord, kABPersonJobTitleProperty);
    personemail = ABRecordCopyValue(aRecord, kABPersonEmailProperty);
    personwebsite = ABRecordCopyValue(aRecord, kABPersonURLProperty);
    personcontact = ABRecordCopyValue(aRecord, kABPersonPhoneProperty);

    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);

    if(isAdded){

    NSLog(@\"added..\");
    }
    if (error != NULL) {
    NSLog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;

    BOOL isSaved = ABAddressBookSave (addressBook, &error);

    if(isSaved) {

    NSLog(@\"saved..\");
    UIAlertView *alertOnChoose = [[UIAlertView alloc] initWithTitle:@\"Phone added successfully to your addressbook\" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@\"Ok\", nil];
    [alertOnChoose show];
    [alertOnChoose release];
    }

    if (error != NULL) {

    NSLog(@\"ABAddressBookSave %@\", error);
    UIAlertView *alertOnChoose = [[UIAlertView alloc] initWithTitle:@\"Unable to save this time\" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@\"Ok\", nil];
    [alertOnChoose show];
    [alertOnChoose release];
    }

    CFRelease(aRecord);
    CFRelease(personname);
    CFRelease(personcompind);
    CFRelease(personcontact);
    CFRelease(personemail);
    CFRelease(personfunction);
    CFRelease(personwebsite);
    CFRelease(addressBook);


    Here username, usercontact, usercompany, useremail, userwebsite etc are NSString variables...
    Thanx & Regards-

    Vaibhav Saran | Software Engineer

    Neo Sypher Systems Pvt. Ltd.

    New Delhi | India

    Websites:

    <font color="
  • ddotddot Posts: 3New Users
    thx for that code, i am really new to obj c and mac programming, so i am still confused as to how to implement this, i have all those fields displayed in a textfeild right now and i want the user to be able to edit those fields and save it back to the address book. here is what i have so far in my implementation file:


    #import "DetailViewController.h"
    #import "RootViewController.h"

    @implementation DetailViewController

    @synthesize firstName;
    @synthesize lastName;
    @synthesize addresslabel;
    @synthesize myemail;
    @synthesize mobile;
    @synthesize homephone;
    @synthesize birthday;
    @synthesize iphone;
    @synthesize toolbar;
    @synthesize detailDescriptionLabel;
    @synthesize detailItem;

    @synthesize popoverController;

    - (IBAction)showPicker:(id)sender {

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];

    picker.peoplePickerDelegate = self;

    [self presentModalViewController:picker animated:YES];

    [picker release];
    }


    - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {

    [self dismissModalViewControllerAnimated:YES];

    }

    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
    {
    NSString * fname = [[[NSString alloc] init] autorelease];
    NSString * lname = [[[NSString alloc] init] autorelease];

    fname = (NSString *)ABRecordCopyValue(person,kABPersonFirstNameProperty);
    lname = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
    mobile.text = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);


    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    if (emails && ABMultiValueGetCount(emails) == 1) {
    NSString *email = (NSString*)ABMultiValueCopyValueAtIndex(emails, 0);


    NSString * addy = [[[NSString alloc] init] autorelease];
    ABMutableMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty);

    for (CFIndex addyIndex = 0; addyIndex < ABMultiValueGetCount(addresses); addyIndex++) {
    NSString * addyType = (NSString *) ABMultiValueCopyLabelAtIndex(addresses, addyIndex);

    NSRange addySearchRange = [addyType rangeOfString:@"home" options:NSCaseInsensitiveSearch]; //specify what kind of address you're looking

    if (addySearchRange.length){
    CFDictionaryRef AddyRef = ABMultiValueCopyValueAtIndex(addresses, addyIndex);

    CFStringRef addyStreet = CFDictionaryGetValue(AddyRef, kABPersonAddressStreetKey);
    if ([(NSString *)addyStreet length]){
    addy = [addy stringByAppendingFormat:(NSString *)addyStreet];
    }

    CFStringRef addyCity = CFDictionaryGetValue(AddyRef, kABPersonAddressCityKey);
    if ([(NSString *)addyCity length]){
    addy = [addy stringByAppendingFormat:(NSString *)@", "];
    addy = [addy stringByAppendingFormat:(NSString *)addyCity];
    }

    CFStringRef addyState = CFDictionaryGetValue(AddyRef, kABPersonAddressStateKey);
    if ([(NSString *)addyState length]){
    addy = [addy stringByAppendingFormat:(NSString *)@", "];
    addy = [addy stringByAppendingFormat:(NSString *)addyState];
    }

    CFStringRef addyZip = CFDictionaryGetValue(AddyRef, kABPersonAddressZIPKey);
    if ([(NSString *)addyZip length]){
    addy = [addy stringByAppendingFormat:(NSString *)@", "];
    addy = [addy stringByAppendingFormat:(NSString *)addyZip];
    }

    CFStringRef addyCountry = CFDictionaryGetValue(AddyRef, kABPersonAddressCountryKey);
    if ([(NSString *)addyCountry length]){
    addy = [addy stringByAppendingFormat:(NSString *)@", "];
    addy = [addy stringByAppendingFormat:(NSString *)addyCountry];
    }

    CFRelease(AddyRef);

    }

    }

    self.firstName.text = fname;
    self.lastName.text = lname;
    self.addresslabel.text = addy;
    self.myemail.text = email;

    [self dismissModalViewControllerAnimated:YES];

    return NO;
    }
    }

    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
    shouldContinueAfterSelectingPerson:(ABRecordRef)person
    property:(ABPropertyID)property
    identifier:(ABMultiValueIdentifier)identifier {

    }

    any suggestions, thank you for your quick response
  • JonathanSiboniJonathanSiboni Posts: 2New Users
    david_david;65925 said:
    this is the working example

    ABRecordRef aRecord = ABPersonCreate(); 
    CFErrorRef anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty,
    CFSTR(\"Jijo\"), &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty,
    CFSTR(\"Pulikkottil\"), &anError);
    if (anError != NULL) {

    NSLog(@\"error while creating..\");
    }
    CFStringRef firstName, lastName;
    firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
    lastName = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);




    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    BOOL isAdded = ABAddressBookAddRecord (
    addressBook,
    aRecord,
    &error
    );

    if(isAdded){

    NSLog(@\"added..\");
    }
    if (error != NULL) {
    NSLog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;

    BOOL isSaved = ABAddressBookSave (
    addressBook,
    &error
    );

    if(isSaved){

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

    if (error != NULL) {
    NSLog(@\"ABAddressBookSave %@\", error);
    }

    CFRelease(aRecord);
    CFRelease(firstName);
    CFRelease(lastName);
    CFRelease(addressBook);



    don't forget add AddressBook.Framework
    Ref: AddressBookProgrammingGuideforiPhone.pdf
    Ok this code works , but how add informations in the contact like mobile phone,
    email, company name ..?
  • david_daviddavid_david Posts: 372Registered Users
    Also posted here


    #import <AddressBook/AddressBook.h>

    -(NSUInteger) copyContactToAB{

    NSUInteger addressbookId = 0;

    UIImage *imgContact = nil;//[UIImage imageNamed:@\"test.png\"];
    NSString *companyName = @\"Test\";

    //Initialize the variables..

    NSString *firstName= @\"Hanoch\";
    NSString *lastName= @\"J\";
    NSString *workPhone = nil;
    NSString *workFax = nil;
    NSString *mobile = nil;
    NSString *homePhone = nil;
    NSString *workEmail = nil;


    //work address
    NSString *address1_street = nil;
    NSString *address1_city = nil;
    NSString *address1_state = nil;
    NSString *address1_postalcode = nil;
    NSString *address1_country = nil;

    //home address
    NSString *address2_street = nil;
    NSString *address2_city = nil;
    NSString *address2_state = nil;
    NSString *address2_postalcode = nil;
    NSString *address2_country = nil;


    ABRecordRef aRecord = ABPersonCreate();
    CFErrorRef anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &anError);
    if(companyName){
    ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &anError);
    }





    if (anError != NULL) {

    MELog(@\"error while creating..%@\", anError);
    }

    if(imgContact){


    NSData *data = UIImagePNGRepresentation(imgContact);
    if(ABPersonSetImageData(aRecord, (CFDataRef)data, &anError)){


    }


    }


    //(@\"adding email\");
    if(workEmail){
    ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emails, workEmail, kABWorkLabel, NULL);

    ABRecordSetValue(aRecord, kABPersonEmailProperty, emails, &anError);

    CFRelease(emails);
    }

    //(@\"adding phonee\");
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);


    if(workPhone) ABMultiValueAddValueAndLabel(multi, workPhone, kABWorkLabel, NULL) ;
    if(workFax) ABMultiValueAddValueAndLabel(multi, workFax, kABPersonPhoneWorkFAXLabel,NULL) ;//&&
    if(mobile) ABMultiValueAddValueAndLabel(multi, mobile, kABPersonPhoneMobileLabel,NULL);
    if(homePhone) ABMultiValueAddValueAndLabel(multi, homePhone, kABHomeLabel,NULL);

    if(workPhone || workFax || mobile || homePhone){

    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
    if (anError != NULL) { MELog(@\"error while creating ABMutableMultiValueRef..%@\", anError); }

    }

    CFRelease(multi);



    //add work adress
    ABMutableMultiValueRef multiAddress1 = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

    NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init];
    if(address1_street) [addressDictionary1 setObject:address1_street forKey:(NSString *) kABPersonAddressStreetKey];
    if(address1_city) [addressDictionary1 setObject:address1_city forKey:(NSString *)kABPersonAddressCityKey];
    if(address1_state) [addressDictionary1 setObject:address1_state forKey:(NSString *)kABPersonAddressStateKey];
    if(address1_postalcode) [addressDictionary1 setObject:address1_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
    if(address1_country) [addressDictionary1 setObject:address1_country forKey:(NSString *)kABPersonAddressCountryKey];
    //[addressDictionary setObject:@\"us\" forKey:(NSString *)kABPersonAddressCountryCodeKey];

    bool didAdd = NO;

    if([addressDictionary1 count] > 0){
    didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary1, kABWorkLabel, NULL);
    }

    [addressDictionary1 release];

    //add home address

    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    if(address2_street) [addressDictionary2 setObject:address2_street forKey:(NSString *) kABPersonAddressStreetKey];
    if(address2_city) [addressDictionary2 setObject:address2_city forKey:(NSString *)kABPersonAddressCityKey];
    if(address2_state) [addressDictionary2 setObject:address2_state forKey:(NSString *)kABPersonAddressStateKey];
    if(address2_postalcode) [addressDictionary2 setObject:address2_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
    if(address2_country) [addressDictionary2 setObject:address2_country forKey:(NSString *)kABPersonAddressCountryKey];
    //[addressDictionary setObject:@\"us\" forKey:(NSString *)kABPersonAddressCountryCodeKey];

    if([addressDictionary2 count] > 0 ){
    didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary2, kABHomeLabel, NULL);


    }
    if(didAdd){
    //(@\"home address added\");
    anError = NULL;
    ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAddress1, &anError);
    if (anError != NULL) { MELog(@\"error while creating home address..%@\", anError); }
    }

    CFRelease(multiAddress1);
    [addressDictionary2 release];
    addressDictionary2 = nil;


    ////////////

    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    /*BOOL isAdded =*/ ABAddressBookAddRecord (addressBook, aRecord, &error);


    if (error != NULL) {

    MELog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;



    if(ABAddressBookSave ( addressBook, &error)){


    addressbookId = ABRecordGetRecordID (aRecord);

    }

    if (error != NULL) {
    MELog(@\"ABAddressBookSave %@\", error);
    }

    CFRelease(aRecord);

    CFRelease(addressBook);


    return addressbookId;

    }

    lazy blogs

    The Lord's holy name be praised.
  • JonathanSiboniJonathanSiboni Posts: 2New Users
    david_david;439115 said:
    Also posted here


    #import <AddressBook/AddressBook.h>

    -(NSUInteger) copyContactToAB{

    NSUInteger addressbookId = 0;

    UIImage *imgContact = nil;//[UIImage imageNamed:@\"test.png\"];
    NSString *companyName = @\"Test\";

    //Initialize the variables..

    NSString *firstName= @\"Hanoch\";
    NSString *lastName= @\"J\";
    NSString *workPhone = nil;
    NSString *workFax = nil;
    NSString *mobile = nil;
    NSString *homePhone = nil;
    NSString *workEmail = nil;


    //work address
    NSString *address1_street = nil;
    NSString *address1_city = nil;
    NSString *address1_state = nil;
    NSString *address1_postalcode = nil;
    NSString *address1_country = nil;

    //home address
    NSString *address2_street = nil;
    NSString *address2_city = nil;
    NSString *address2_state = nil;
    NSString *address2_postalcode = nil;
    NSString *address2_country = nil;


    ABRecordRef aRecord = ABPersonCreate();
    CFErrorRef anError = NULL;
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, firstName, &anError);
    ABRecordSetValue(aRecord, kABPersonLastNameProperty, lastName, &anError);
    if(companyName){
    ABRecordSetValue(aRecord, kABPersonOrganizationProperty, companyName, &anError);
    }





    if (anError != NULL) {

    MELog(@\"error while creating..%@\", anError);
    }

    if(imgContact){


    NSData *data = UIImagePNGRepresentation(imgContact);
    if(ABPersonSetImageData(aRecord, (CFDataRef)data, &anError)){


    }


    }


    //(@\"adding email\");
    if(workEmail){
    ABMutableMultiValueRef emails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emails, workEmail, kABWorkLabel, NULL);

    ABRecordSetValue(aRecord, kABPersonEmailProperty, emails, &anError);

    CFRelease(emails);
    }

    //(@\"adding phonee\");
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);


    if(workPhone) ABMultiValueAddValueAndLabel(multi, workPhone, kABWorkLabel, NULL) ;
    if(workFax) ABMultiValueAddValueAndLabel(multi, workFax, kABPersonPhoneWorkFAXLabel,NULL) ;//&&
    if(mobile) ABMultiValueAddValueAndLabel(multi, mobile, kABPersonPhoneMobileLabel,NULL);
    if(homePhone) ABMultiValueAddValueAndLabel(multi, homePhone, kABHomeLabel,NULL);

    if(workPhone || workFax || mobile || homePhone){

    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
    if (anError != NULL) { MELog(@\"error while creating ABMutableMultiValueRef..%@\", anError); }

    }

    CFRelease(multi);



    //add work adress
    ABMutableMultiValueRef multiAddress1 = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

    NSMutableDictionary *addressDictionary1 = [[NSMutableDictionary alloc] init];
    if(address1_street) [addressDictionary1 setObject:address1_street forKey:(NSString *) kABPersonAddressStreetKey];
    if(address1_city) [addressDictionary1 setObject:address1_city forKey:(NSString *)kABPersonAddressCityKey];
    if(address1_state) [addressDictionary1 setObject:address1_state forKey:(NSString *)kABPersonAddressStateKey];
    if(address1_postalcode) [addressDictionary1 setObject:address1_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
    if(address1_country) [addressDictionary1 setObject:address1_country forKey:(NSString *)kABPersonAddressCountryKey];
    //[addressDictionary setObject:@\"us\" forKey:(NSString *)kABPersonAddressCountryCodeKey];

    bool didAdd = NO;

    if([addressDictionary1 count] > 0){
    didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary1, kABWorkLabel, NULL);
    }

    [addressDictionary1 release];

    //add home address

    NSMutableDictionary *addressDictionary2 = [[NSMutableDictionary alloc] init];
    if(address2_street) [addressDictionary2 setObject:address2_street forKey:(NSString *) kABPersonAddressStreetKey];
    if(address2_city) [addressDictionary2 setObject:address2_city forKey:(NSString *)kABPersonAddressCityKey];
    if(address2_state) [addressDictionary2 setObject:address2_state forKey:(NSString *)kABPersonAddressStateKey];
    if(address2_postalcode) [addressDictionary2 setObject:address2_postalcode forKey:(NSString *)kABPersonAddressZIPKey];
    if(address2_country) [addressDictionary2 setObject:address2_country forKey:(NSString *)kABPersonAddressCountryKey];
    //[addressDictionary setObject:@\"us\" forKey:(NSString *)kABPersonAddressCountryCodeKey];

    if([addressDictionary2 count] > 0 ){
    didAdd = ABMultiValueAddValueAndLabel(multiAddress1, addressDictionary2, kABHomeLabel, NULL);


    }
    if(didAdd){
    //(@\"home address added\");
    anError = NULL;
    ABRecordSetValue(aRecord, kABPersonAddressProperty, multiAddress1, &anError);
    if (anError != NULL) { MELog(@\"error while creating home address..%@\", anError); }
    }

    CFRelease(multiAddress1);
    [addressDictionary2 release];
    addressDictionary2 = nil;


    ////////////

    ABAddressBookRef addressBook;
    CFErrorRef error = NULL;
    addressBook = ABAddressBookCreate();

    /*BOOL isAdded =*/ ABAddressBookAddRecord (addressBook, aRecord, &error);


    if (error != NULL) {

    MELog(@\"ABAddressBookAddRecord %@\", error);
    }
    error = NULL;



    if(ABAddressBookSave ( addressBook, &error)){


    addressbookId = ABRecordGetRecordID (aRecord);

    }

    if (error != NULL) {
    MELog(@\"ABAddressBookSave %@\", error);
    }

    CFRelease(aRecord);

    CFRelease(addressBook);


    return addressbookId;

    }

    Ok , thanks but how can I use this code for an action ?
  • david_daviddavid_david Posts: 372Registered Users
    JonathanSiboni;439224 said:
    Ok , thanks but how can I use this code for an action ?
    I am not sure what you are asking..

    Change the method by passing the arguments as NSString or in a NSDictionary.
    And can call this method from your action method.
    lazy blogs

    The Lord's holy name be praised.
Sign In or Register to comment.