Add my info to users address book

Discussion in 'iOS Programming' started by primedime, Jun 24, 2011.

  1. primedime macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #1
    Hey guys,

    I was building an app for myself and I wanted to be able to create a uibutton that when pressed by a user it would add my contact information to the users address book. Any suggestions? I have been searching for a few hours now without finding exactly what i'm looking for. Thanks.
     
  2. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #3
    Yes, I have many times and spent a good amount of time researching this last night. I think this is what I need to use?

    ABAddressBookAddRecord

    But I am not understanding how I combine it with and action like pressing a UIButton to bring up an alert that asks if you want to add "Roy Jossfolk" to your address book. on "OK" it would then add it.

    Code:
    bool ABAddressBookAddRecord (
       ABAddressBookRef addressBook,
       ABRecordRef record,
       CFErrorRef *error
    );
    
    This is something I saw as well but it isn't making much sense to me, it doesn't look like a lot of the other things I have done. Am I headed in the right direction here or am I totally off?
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes you are heading in the correct direction. It probably looks different as it is a C rather than Objective-C API. You did ensure that you understood C before learning Objective-C right? If not this may be a good time to learn C.
     
  4. primedime, Jun 25, 2011
    Last edited by a moderator: Jun 25, 2011

    primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #5
    No, I have not learned C as of now. I have learned a good amount of OBJ-C (in my opinion), enough to be able to create certain types of apps. I typically reach a point in each app where I must research heavily and when that fails I typically end up here, lol. I suppose this is one of those times! Is there anything specific in a C book that I should be looking at to understand what I must do next? Or has anyone done this before and want to give me a little help on how they figured it out?

    And just so anyone trying to help knows, I already know about the IBActions I must set and all that. I have already used that to make buttons and stuff interactive on my app already. Once I have those IBActions set up, I need to set up an alert for when the UIButton is pushed. In that alert you can either press cancel and go back or OK and it then adds my pre determined contact info in to their address book. I saw a few tutorials on how to manually add contacts to the address boo from an app, is this process and the one I want to accomplish similar?
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    You should look at C functions. ABAddressBookAddRecord is a function.

    If you already know how to do those parts, then you can write and test the code for it. Then post the code.

    When you get to the part in your code where you intend to add the info to the address book, simply put an NSLog() that prints a message like "Add to address book goes here." Make sure that appears in the posted code, so we know what you intend.

    It might be. You haven't shown us any URLs, so we can't usefully comment on what we haven't seen.
     
  6. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #7
    Ok, cool. Well I am running to a quick meeting and then I will come back and begin to put this together. I will post the code once I get to the point where I am stuck. Thanks!
     
  7. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #8
    OK, I have to the point where you push on the add address uibutton and an alert pops up saying "Add Roy Jossfolk Jr to you address book?" with a "Cancel" and "YES" option.

    Here are the .h/.m codes:

    MainViewController.h
    Code:
    #import "FlipsideViewController.h"
    #import "YoutubeViewController.h"
    #import "FacebookViewController.h"
    #import "LinkedinViewController.h"
    #import "TwitterViewController.h"
    #import <MessageUI/MessageUI.h>
    #import <AddressBook/AddressBook.h>
    #import <AddressBookUI/AddressBookUI.h>
    
    @interface MainViewController : UIViewController 
    <FlipsideViewControllerDelegate,MFMailComposeViewControllerDelegate> {
    
    }
    - (IBAction)tweetView;
    - (IBAction)faceView;
    - (IBAction)linkView;
    - (IBAction)switchView;
    - (IBAction)showInfo:(id)sender;
    - (IBAction)email:(id)sender;
    - (IBAction)addContact:(id)sender;
    - (IBAction)callMe:(id)sender;
    @end
    
    

    MainViewController.m (just the code pertaining to this action)

    Code:
    #import "MainViewController.h"
    
    @implementation MainViewController
    
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    */
    
    //add my contact info to address book
    -(IBAction)addContact:(id)sender;
    {
        UIAlertView *alertView=
        [[UIAlertView alloc] initWithTitle:@"Add Roy Jossfolk Jr. to your address book?" 
                                   message:nil
                                  delegate:nil 
                         cancelButtonTitle:@"Cancel" 
                         otherButtonTitles:@"YES", nil];
        [alertView show];
        [alertView release];
    }
    

    So now I believe I need to set it so that if "YES" is selected from the alertView my info (name, phone number, email, website) will be saved to their address book.

    @robbieduncan said previously that using this code:

    Code:
    bool ABAddressBookAddRecord (
       ABAddressBookRef addressBook,
       ABRecordRef record,
       CFErrorRef *error
    );
    
    
    Was heading in the right direction. So I suppose could someone help me figure this out?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    That's a function signature. You have to call that function passing the three parameters (with the correct values obviously).

    Read the Address Book Programming Guide.

    Look at the QuickContacts sample code (only after you have read and understood the guide)
     
  9. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #10
    ok, that was pretty intense! I actually got it, thanks everyone. Now which do you suggest; Having an Alert asking them if they are sure they want to add my contact info? or, Have an alert that it was successful? or both?

    Here is my new code:

    MainViewController.m (just the relevant code)

    Code:
    //add my contact info to address book
    -(IBAction)addContact:(id)sender;
    {
        ABAddressBookRef addressBook = ABAddressBookCreate();
        ABRecordRef person = ABPersonCreate();
        
        ABRecordSetValue(person, kABPersonFirstNameProperty, @"Roy" , nil);
        ABRecordSetValue(person, kABPersonLastNameProperty, @"Jossfolk, Jr.", nil);
        ABRecordSetValue(person, kABPersonJobTitleProperty, @"Marketing Expert", nil);
        
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"9546101824", kABPersonPhoneMobileLabel, nil);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
        ABMultiValueAddValueAndLabel(emailMultiValue, @"info@royjossfolk.com", kABWorkLabel, nil);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        
        ABAddressBookAddRecord(addressBook, person, nil);
        ABAddressBookSave(addressBook, nil);
        
        CFRelease(person);
        CFRelease(phoneNumberMultiValue);
        CFRelease(emailMultiValue);
        /*
        UIAlertView *alertView=
        [[UIAlertView alloc] initWithTitle:@"Add Roy Jossfolk Jr. to your address book?" 
                                   message:nil
                                  delegate:nil 
                         cancelButtonTitle:@"Cancel" 
                         otherButtonTitles:@"YES", nil];
        [alertView show];
        [alertView release];
         */
    }
    
    
    
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    You should ask them and respect their decision if they say no. I'm pretty sure Apple won't approve your app if you do it the other way.
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    1. Use a non-nil delegate. It should only add the info to the address book when the clicked button is YES.

    2. What will happen if the user clicks the "Add" button, clicks "Yes" in the alert, and your AB record previously existed in the address book? Are you going to add another record? Overwrite the existing one? Replace an existing record that the user may have edited data in?

    3. Have you considered using an ABNewPersonViewController or ABUnknownPersonViewController?
    Suppose it works like this: the user clicks your "Add" button, you populate the new person controller with data, then present it. Affirming adds the data, declining discards it. You can further improve the logic so it looks for existing AB records with your data, and presents a different AB UI controller for editing an existing record.
     
  12. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #13
    1) Where do I add this? on all the'nil'(s) within the code for the actual name and number or do I just change the delegate in the alertView part of the code? I added (id)sender to the alertView code and it still doesn't go by the options but does bring up the alert.

    here is what I got:

    MainViewController.m

    Code:
    
    //add my contact info to address book
    -(IBAction)addContact:(id)sender;
    {
        ABAddressBookRef addressBook = ABAddressBookCreate();
        ABRecordRef person = ABPersonCreate();
        
        ABRecordSetValue(person, kABPersonFirstNameProperty, @"Roy" , nil);
        ABRecordSetValue(person, kABPersonLastNameProperty, @"Jossfolk, Jr.", nil);
        ABRecordSetValue(person, kABPersonJobTitleProperty, @"Marketing Expert", nil);
        
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABPersonURLProperty
                                                                         );
        ABMultiValueAddValueAndLabel(urlMultiValue, @"www.RoyJossfolk.com", kABPersonHomePageLabel, nil
                                     );
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
        ABMultiValueAddValueAndLabel(phoneNumberMultiValue, @"9546101824", kABPersonPhoneMobileLabel, nil);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
        ABMultiValueAddValueAndLabel(emailMultiValue, @"info@royjossfolk.com", kABWorkLabel, nil);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        
        ABAddressBookAddRecord(addressBook, person, nil);
        ABAddressBookSave(addressBook, nil);
        
        CFRelease(person);
        CFRelease(phoneNumberMultiValue);
        CFRelease(emailMultiValue);
      
        UIAlertView *alertView=
        [[UIAlertView alloc] initWithTitle:@"Add Roy Jossfolk Jr. to your address book?" 
                                   message:nil
                                  delegate:(id)sender
                         cancelButtonTitle:@"Cancel" 
                         otherButtonTitles:@"YES", nil];
        [alertView show];
        [alertView release];
        
    }
    
    
    
     
  13. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #14
    2) that is a very good question. I think I will try it but it seems complicated.

    3) I started to look at it but I will have to keep re reading about it and maybe see some examples before I think I can implement it.
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    Your posted code has a logic problem: you're adding the record, and then presenting the alert. That's backwards.

    Code:
    -(IBAction)addContact:(id)sender;
    {
        ... bunch of code snipped ...
        
        [COLOR="Red"]ABAddressBookAddRecord(addressBook, person, nil); // adds the record[/COLOR]
    [COLOR="red"]    ABAddressBookSave(addressBook, nil); // commits/saves the data[/COLOR]    
        CFRelease(person);
        CFRelease(phoneNumberMultiValue);
        CFRelease(emailMultiValue);
      
        UIAlertView *alertView=
        [[UIAlertView alloc] initWithTitle:@"Add Roy Jossfolk Jr. to your address book?" 
                                   message:nil
                                  delegate:[COLOR="Blue"](id)sender[/COLOR]
                         cancelButtonTitle:@"Cancel" 
                         otherButtonTitles:@"YES", nil];
        [COLOR="red"][alertView show];  // presents the alert AFTER data already saved[/COLOR]
        [alertView release];
        
    }
    
    Also, if sender (see blue hilite) doesn't implement the correct delegate protocol (and my first guess is it doesn't), then it won't do anything when the alert is dismissed.

    The structure of this code suggests you aren't sure what the delegate of a UIAlertView actually does. Which suggests you haven't read the class reference doc for UIAlertView.

    And just wondering: have you considered a UIActionSheet instead of UIAlertView?
     
  15. primedime thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #16
    Yes I did consider the Action Sheet over the Alert View, not sure which is easier to implement. which do you suggest?

    Right now I am reading more in to the UIAlertView Reference Docs and the UIAlertViewDelegate Docs to figure out where everything needs to be.

    If anyone knows a good sample project or maybe an online tutorial using the alertView the way I am that would be appreciated. The provided documentation is hard to understand at times, getting better everyday though.
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #17
    I suggest the one that makes the most sense for the user. "Easier to implement" means nothing to users.

    The UIAlertView class reference has links to Related sample code.
     

Share This Page