通訊錄見解

想要訪問通訊錄首先得添加AddressBook.frameWork和AddressBookUI.frameWork兩個框架

1查看

-(void)showPeoplePickerController{

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController allocinit];

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 allocinit] 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,@"[email protected]",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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章