Posted By

espinallab on 05/06/10


Tagged

iphone addressbook ABAddressBook ABAddressBookCreate


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

avanadra
umang_nine
rvachere
sb
Tintenklecks
cnathan


Adding entry to AddressBook iPhone


 / Published in: Objective C
 

URL: http://www.espinallab.com

Add the following framework "AddressBook.framework" to your project

Make sure to import these header files.

import "AddressBook/ABAddressBook.h" import "AddressBook/ABPerson.h" import "AddressBook/ABMultiValue.h"
  1. //This code is working properly in my project. Hope it helps you too.
  2.  
  3. #pragma mark -
  4. #pragma mark addToAddressBook
  5.  
  6. - (IBAction)addToAddressBook
  7. {
  8. ABAddressBookRef addressBook = ABAddressBookCreate();
  9. ABRecordRef person = ABPersonCreate();
  10. CFErrorRef error = NULL;
  11.  
  12. ABRecordSetValue(person, kABPersonOrganizationProperty, venue.name, &error);
  13.  
  14. // a single url as the home page
  15. ABMutableMultiValueRef urlMultiValue =
  16. ABMultiValueCreateMutable(kABStringPropertyType);
  17. ABMultiValueAddValueAndLabel(urlMultiValue, @"http://www.clubplanet.com",
  18. kABPersonHomePageLabel, NULL);
  19. ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, &error);
  20. CFRelease(urlMultiValue);
  21.  
  22. if (venue.phone) {
  23. ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
  24. ABMultiValueAddValueAndLabel(multiPhone, venue.phone, kABPersonPhoneMainLabel, NULL);
  25. ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error);
  26. CFRelease(multiPhone);
  27.  
  28. }
  29.  
  30. ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
  31.  
  32. //Create a Disctionary Array to hold the address
  33. NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
  34.  
  35. if (venue.address) [addressDictionary setObject:venue.address forKey:(NSString *)kABPersonAddressStreetKey];
  36. if (venue.city) [addressDictionary setObject:venue.city forKey:(NSString *)kABPersonAddressCityKey];
  37. if (venue.state) [addressDictionary setObject:venue.state forKey:(NSString *)kABPersonAddressStateKey];
  38. if (venue.zip) [addressDictionary setObject:venue.zip forKey:(NSString *)kABPersonAddressZIPKey];
  39. ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL);
  40. ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&error);
  41. CFRelease(multiAddress);
  42.  
  43. ABAddressBookAddRecord(addressBook, person, &error);
  44. ABAddressBookSave(addressBook, &error);
  45.  
  46. if (error != NULL)
  47. {
  48.  
  49. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
  50. message:@"Could not create unknown user"
  51. delegate:nil
  52. cancelButtonTitle:@"Cancel"
  53. otherButtonTitles:nil];
  54. [alert show];
  55. [alert release];
  56. } else {
  57. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add To Contacts"
  58. message:[NSString stringWithFormat:@"%@ was added to your contact successfully!", venue.name]
  59. delegate:nil
  60. cancelButtonTitle:@"OK"
  61. otherButtonTitles:nil];
  62. [alert show];
  63. [alert release];
  64. }
  65.  
  66.  
  67. CFRelease(person);
  68. }

Report this snippet  

You need to login to post a comment.