AddressBookUI.framework管理通讯录

//创建一个新的联系人

- (IBAction)addBK:(id)sender {

ABNewPersonViewController* controller=[[ABNewPersonViewControlleralloc]init];

controller.newPersonViewDelegate=self;

UINavigationController* nav=[[UINavigationControlleralloc]initWithRootViewController:controller];

[selfpresentViewController:navanimated:YEScompletion:nil];

}

- (IBAction)selectBK:(id)sender {

//用于让用于选择某个联系人或选择某个联系人信息项

ABPeoplePickerNavigationController* controller=[[ABPeoplePickerNavigationControlleralloc]init];

controller.peoplePickerDelegate=self;

[selfpresentViewController:controlleranimated:YEScompletion:NULL];

}

- (IBAction)selectBKByID:(id)sender {

ABAddressBookRefab=ABAddressBookCreateWithOptions(NULL,NULL);

//获取ID为1的联系人纪录

ABRecordRefrec=ABAddressBookGetPersonWithRecordID(ab,1);

ABPersonViewController* controller=[[ABPersonViewControlleralloc]init];

controller.allowsActions=YES;

controller.allowsEditing=YES;

controller.personViewDelegate=self;

controller.displayedPerson=rec;

UINavigationController* nav=[[UINavigationControlleralloc]initWithRootViewController:controller];

[selfpresentViewController:navanimated:YEScompletion:nil];

}

//未知联系人

- (IBAction)otherBK:(id)sender {

ABUnknownPersonViewController* controller=[[ABUnknownPersonViewControlleralloc]init];

controller.unknownPersonViewDelegate=self;

//设置显示标准动作

controller.allowsActions=YES;

//设置允许将该位置的联系人添加到地址簿

controller.allowsAddingToAddressBook=YES;

//创建依靠新的纪录

ABRecordRefrecord=ABPersonCreate();

ABRecordSetValue(record,kABPersonFirstNameProperty,(__bridgeCFTypeRef)@"阿芙罗狄忒",NULL);

ABRecordSetValue(record,kABPersonLastNameProperty, (__bridgeCFTypeRef)@"众神",NULL);

ABMutableMultiValueRefmulti=ABMultiValueCreateMutable(kABPersonPhoneProperty);

ABMultiValueAddValueAndLabel(multi, (__bridgeCFTypeRef)@"美之神",(__bridgeCFTypeRef)@"工作",NULL);

controller.displayedPerson=record;

UINavigationController* nav=[[UINavigationControlleralloc]initWithRootViewController:controller];

[selfpresentViewController:navanimated:YEScompletion:NULL];

}

//----- ABNewPersonViewControllerDelegate

-(void)newPersonViewController:(ABNewPersonViewController*)newPersonView didCompleteWithNewPerson:(ABRecordRef)person

{

if(person) {

NSString* firstName=(__bridgeNSString*)(ABRecordCopyValue(person,kABPersonFirstNameProperty));

[selfshowAlert:[NSStringstringWithFormat:@"[%@]联系人被保存",firstName]];

}

[newPersonView.navigationControllerdismissViewControllerAnimated:YEScompletion:nil];

}

//-----ABUnknownPersonViewControllerDelegate

-(void)unknownPersonViewController:(ABUnknownPersonViewController*)unknownCardViewController didResolveToPerson:(ABRecordRef)person{

if(person) {

NSString* firstName=(__bridgeNSString* )ABRecordCopyValue(person,kABPersonFirstNameProperty);

[selfshowAlert:[NSStringstringWithFormat:@"[%@]联系人被保存",firstName]];

}

[unknownCardViewController.navigationControllerdismissViewControllerAnimated:YEScompletion:nil];

}

//--ABPeoplePickerNavigationControllerDelegate

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{

[peoplePickerdismissViewControllerAnimated:YEScompletion:^{

NSString* firstName=(__bridgeNSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

[selfshowAlert:[NSStringstringWithFormat:@"您选中了[%@]联系人",firstName]];

}];

returnNO;

}

//-----------ABPersonViewControllerDelegate

-(BOOL)personViewController:(ABPersonViewController*)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{

//    [self showAlert:[NSString stringWithFormat:@"名字为:%@",ab]];

[personViewController.navigationControllerdismissViewControllerAnimated:YEScompletion:NULL];

returnYES;

}

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController*)peoplePicker{

NSLog(@"用户取消了选择");

}

-(void)showAlert:(NSString*)str{

UIAlertController* alert=[UIAlertControlleralertControllerWithTitle:@"提示"message:strpreferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* action=[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];

[alertaddAction:action];

[selfpresentViewController:alertanimated:YEScompletion:nil];

}

你可能感兴趣的:(AddressBookUI.framework管理通讯录)