通讯录见解

想要访问通讯录首先得添加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,@"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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值