在地址簿中设置街道地址等并让用户将其保存在 iPhone 上的正确方法是什么?
编辑:删除了特定的代码问题并使其更通用
这是一个完整的工作示例,说明如何通过创建 ABRecordRef 并使用视图控制器将其推送到视图中来显示一个人
///////////////////////// 将其连接到自定义操作。
-(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];
}
//////////////////////// 这是构建 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, @"expert.in@computer.com", 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);
[addressDict release];
CFRelease(address);
// End of Address
if (error != NULL)
NSLog(@"Error: %@", error);
[(id)person autorelease];
return person;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
请记住导入这些框架:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
设置委托
ABNewPersonViewControllerDelegate
并将其添加到界面中
ABNewPersonViewController *newPersonController;
从目测来看,我认为不是
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiStringPropertyType);
你要
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABDictionaryPropertyType);