将条目添加到AddressBook iPhone

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将条目添加到AddressBook iPhone相关的知识,希望对你有一定的参考价值。

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. }

以上是关于将条目添加到AddressBook iPhone的主要内容,如果未能解决你的问题,请参考以下文章

Iphone AddressBook Framework - 人物属性

从 AddressBook.framework 生成 vCard

iphone - writeToFile 没有将新条目保存到 plist

iphone通讯录的备份与恢复

iOS AddressBook - 获取联系人图片崩溃

将条目添加到列表的开头并删除最后一个