views:

1668

answers:

3

What is the correct way to set street address etc in addressbook and let the user save it on iphone?

EDIT: removed the specific code-problem and made it more general

A: 

From eyeballing it, I think instead of

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiStringPropertyType);

you want

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType);
Josh Bleecher Snyder
You pointed me in the right direction. It should be kABMultiDictionaryPropertyType.
Fossli
A: 

I found the solution to how to set address. Gotta use an NSMutableDictionary.. Here's a working sample!

// Start of Address
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);  
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
[addressDict setObject:@"The awesome road numba 1" forKey:(NSString *)kABPersonAddressStreetKey]; 
[addressDict setObject:@"0568" forKey:(NSString *)kABPersonAddressZIPKey]; 
[addressDict setObject:@"Oslo" forKey:(NSString *)kABPersonAddressCityKey]; 
ABMultiValueAddValueAndLabel(address, addressDict, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, address, &error); 
CFRelease(address); 
// End of Address
Fossli
+3  A: 

This is a complete working sample of how to show an person by creating an ABRecordRef and pushing it into the view using an viewcontroller

///////////////////////////// Hook it up to a custom action.

-(IBAction)addToAddressbook:(id)sender{  
    ABUnknownPersonViewController *unknownPersonViewController = [[ABUnknownPersonViewController alloc] init];
    unknownPersonViewController.displayedPerson = (ABRecordRef)[self buildContactDetails];
    unknownPersonViewController.allowsAddingToAddressBook = YES;
    [self.navigationController pushViewController:unknownPersonViewController animated:YES];
    [unknownPersonViewController release]; 
}

//////////////////////////// This is the guy that builds the ABrecordRef

- (ABRecordRef)buildContactDetails {
    NSLog(@"building contact details");
    ABRecordRef person = ABPersonCreate(); 
    CFErrorRef  error = NULL;  

    // firstname
    ABRecordSetValue(person, kABPersonFirstNameProperty, @"Don Juan", NULL);

    // email
    ABMutableMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(email, @"[email protected]", CFSTR("email"), NULL);
    ABRecordSetValue(person, kABPersonEmailProperty, email, &error);
    CFRelease(email); 

    // Start of Address
    ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);  
    NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
    [addressDict setObject:@"The awesome road numba 1" forKey:(NSString *)kABPersonAddressStreetKey]; 
    [addressDict setObject:@"0568" forKey:(NSString *)kABPersonAddressZIPKey]; 
    [addressDict setObject:@"Oslo" forKey:(NSString *)kABPersonAddressCityKey]; 
    ABMultiValueAddValueAndLabel(address, addressDict, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, address, &error); 
    CFRelease(address); 
    // End of Address

    if (error != NULL) 
     NSLog(@"Error: %@", error);

    [(id)person autorelease];
    return person;
}

//////////////////////////// Wire up in the header:

Remember to import these frameworks:

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

Set the delegate

ABNewPersonViewControllerDelegate

And add this to the interface

ABNewPersonViewController *newPersonController;
Fossli
Thanks, this was a BIG help.
Kevin Fisher
if you want to add phonenumbers etc. irc it's pretty similar to adding mail. just look in to the properties ;)
Fossli