- (void)addContact {
// Creating new entry
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABPersonCreate();
NSString *phone = self.data.phone;
NSString *cellphone = self.data.cellPhone;
NSString *fax = self.data.fax;
NSString *email = self.data.email;
NSString *firstName = self.data.firstName;
NSString *lastName = self.data.lastName;
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName), nil);
ABRecordSetValue(person, kABPersonJobTitleProperty, (__bridge CFTypeRef)(title), nil);
ABRecordSetValue(person, kABPersonDepartmentProperty, (__bridge CFTypeRef)(dept), nil);
ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFTypeRef)(org), nil);
ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(unit), nil);
// Adding phone numbers
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(cellphone), (CFStringRef)@"iPhone", NULL);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(phone), (CFStringRef)@"Work", NULL);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFTypeRef)(fax), (CFStringRef)@"Fax", NULL);
ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
CFRelease(phoneNumberMultiValue);
// Adding emails
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(email), (CFStringRef)@"Work", NULL);
ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
// Adding person to the address book
ABAddressBookAddRecord(addressBook, person, nil);
CFRelease(addressBook);
ABNewPersonViewController *c = [[ABNewPersonViewController alloc] init];
[c setNewPersonViewDelegate:self];
[c setDisplayedPerson:person];
CFRelease(person);
[self.navigationController pushViewController:c animated:YES];
}
使用联系人框架的类似代码是什么?如何将人的详细信息(ABRecordRef person = ABPersonCreate();)加载到* contact(CNMutableContact * contact = [CNMutableContact new];)?
答案 0 :(得分:0)
我建议使用ContactsUI框架而不是旧的AddressBookUI框架。因此,请勿使用ABNewPersonViewController
,而应使用CNContactViewController
:
@import ContactsUI;
@interface ViewController () <CNContactViewControllerDelegate>
@end
@implementation ViewController
- (IBAction)didTapButton:(id)sender {
CNMutableContact *contact = [[CNMutableContact alloc] init];
[contact setGivenName:@"John"];
[contact setFamilyName:@"Doe"];
...
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
controller.delegate = self;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
nav.view.backgroundColor = [UIColor blueColor];
[self presentViewController:nav animated:true completion: nil];
}
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(CNContact *)contact {
[viewController.navigationController dismissViewControllerAnimated:true completion:nil];
}
@end
如今,使用Contacts和ContactsUI框架而不是弃用的AddressBook和AddresssBookUI框架。