将条目添加到AddressBook iPhone
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将条目添加到AddressBook iPhone相关的知识,希望对你有一定的参考价值。
Add the following framework "AddressBook.framework" to your projectMake sure to import these header files.
#import "AddressBook/ABAddressBook.h"
#import "AddressBook/ABPerson.h"
#import "AddressBook/ABMultiValue.h"
//This code is working properly in my project. Hope it helps you too. #pragma mark - #pragma mark addToAddressBook - (IBAction)addToAddressBook { ABAddressBookRef addressBook = ABAddressBookCreate(); ABRecordRef person = ABPersonCreate(); CFErrorRef error = NULL; ABRecordSetValue(person, kABPersonOrganizationProperty, venue.name, &error); // a single url as the home page ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABStringPropertyType); ABMultiValueAddValueAndLabel(urlMultiValue, @"http://www.clubplanet.com", kABPersonHomePageLabel, NULL); ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, &error); CFRelease(urlMultiValue); if (venue.phone) { ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiPhone, venue.phone, kABPersonPhoneMainLabel, NULL); ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone, &error); CFRelease(multiPhone); } ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); //Create a Disctionary Array to hold the address if (venue.address) [addressDictionary setObject:venue.address forKey:(NSString *)kABPersonAddressStreetKey]; if (venue.city) [addressDictionary setObject:venue.city forKey:(NSString *)kABPersonAddressCityKey]; if (venue.state) [addressDictionary setObject:venue.state forKey:(NSString *)kABPersonAddressStateKey]; ABMultiValueAddValueAndLabel(multiAddress, addressDictionary, kABWorkLabel, NULL); ABRecordSetValue(person, kABPersonAddressProperty, multiAddress,&error); CFRelease(multiAddress); ABAddressBookAddRecord(addressBook, person, &error); ABAddressBookSave(addressBook, &error); if (error != NULL) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not create unknown user" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; [alert show]; [alert release]; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add To Contacts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } CFRelease(person); }
以上是关于将条目添加到AddressBook iPhone的主要内容,如果未能解决你的问题,请参考以下文章
Iphone AddressBook Framework - 人物属性
从 AddressBook.framework 生成 vCard