想要访问通讯录首先得添加AddressBook.frameWork和AddressBookUI.frameWork两个框架
1查看
-(void)showPeoplePickerController{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumbernumberWithInt:kABPersonBirthdayProperty], [NSNumber numberWithInt:kABPersonFirstNameProperty],nil];
picker.displayedProperties = displayedItems;//通讯录上显示的信息
// Show the picker
[self presentModalViewController:picker animated:YES];
[picker release];
}
同时要实现代理方法
// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
returnYES;
}
// Does not allow users to perform default actions such as dialing a phone number, when they select a person property.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
returnNO;
}
// Dismisses the people picker and shows the application when users tap Cancel.
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;{
[selfdismissModalViewControllerAnimated:YES];
}
2新增
-(void)showNewPersonViewController{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc]init];
picker.newPersonViewDelegate =self;
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:picker];
[self presentModalViewController:navigation animated:YES];
[picker release];
[navigation release];
}
实现代理方法
#pragma mark ABNewPersonViewControllerDelegate methods
// Dismisses the new-person view controller.
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person{
[selfdismissModalViewControllerAnimated:YES];
}
3编辑
-(void)showPersonViewController{
// Fetch the address book
ABAddressBookRef addressBook = ABAddressBookCreate();
// Search for the person named "Appleseed" in the address book
NSArray *people = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,CFSTR("Appleseed"));
// Display "Appleseed" information if found in the address book
if ((people !=nil) && [people count]){
ABRecordRef person = (ABRecordRef)[people objectAtIndex:0];
ABPersonViewController *picker = [[[ABPersonViewController alloc] init] autorelease];
picker.personViewDelegate =self;
picker.displayedPerson = person;
// Allow users to edit the person’s information
picker.allowsEditing =YES;
[self.navigationController pushViewController:picker animated:YES];
}
else {
// Show an alert if "Appleseed" is not in Contacts
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"Error"
message:@"Could not find Appleseed in the Contacts application"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alertshow];
[alertrelease];
}
[people release];
CFRelease(addressBook);
}
代理方法:- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
4未知
ABRecordRef aContact =ABPersonCreate();
CFErrorRef anError =NULL;
ABMultiValueRef email =ABMultiValueCreateMutable(kABMultiStringPropertyType);
bool didAdd =ABMultiValueAddValueAndLabel(email,@"John-Appleseed@mac.com",kABOtherLabel,NULL);
if (didAdd ==YES){
ABRecordSetValue(aContact,kABPersonEmailProperty, email, &anError);
if (anError ==NULL){
ABUnknownPersonViewController *picker = [[ABUnknownPersonViewControlleralloc]init];
picker.unknownPersonViewDelegate =self;
picker.displayedPerson = aContact;
picker.allowsAddingToAddressBook =YES;
picker.allowsActions =YES;
picker.alternateName =@"John Appleseed";
picker.title =@"John Appleseed";
picker.message =@"Company, Inc";
[self.navigationControllerpushViewController:pickeranimated:YES];
[pickerrelease];
}
else {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"Error"
message:@"Could not create unknown user"
delegate:nil
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alertshow];
[alertrelease];
}
}
CFRelease(email);
CFRelease(aContact);
}
- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person;
@optional
- (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier