在地址簿中设置街道地址等并让用户将其保存在 iphone 上的正确方法是什么?
编辑:删除了特定的代码问题并使其更通用
最佳答案
这是一个完整的工作示例,说明如何通过创建 ABRecordRef 并使用 View Controller 将其推送到 View 中来展示人物
//////////////////////////// 将其连接到自定义操作。
-(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>
设置委托(delegate)
ABNewPersonViewControllerDelegate
并将其添加到界面
ABNewPersonViewController *newPersonController;
关于iphone - 在iphone objective-c中将联系人添加到地址簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1709902/