需求
- 由于之前使用第三方跳转通讯录,点击联系人默认获取第一个手机号,现在需要根据联系人的手机号数量,只有1个,点击该联系人后直接dimiss,如果数量大于1,需要跳转至联系人详情进行选择后dismiss。
前提
未解决前方案
- 如果您已了解AddressBook框架,话不多说上代码:
#import <AddressBook/ABPerson.h>
#import <AddressBookUI/AddressBookUI.h>
- 点击跳转通讯录的事件
- (void)btnClick{
//联系人选择控制器
ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc]init];
//设置代理 ABPeoplePickerNavigationControllerDelegate
nav.peoplePickerDelegate =self;
//弹出视图
[self presentViewController:nav animated:YES completion:nil];
}
- 代理
系统方案实现有两个代理:
1.实现peoplePickerNavigationController:didSelectPerson:
2.实现peoplePickerNavigationController:didSelectPerson: property:identifier:(ABMultiValueIdentifier)identifier
该方法在8以后,是直接跳转详情页,不实现,就会直接走第一种方案。
//ios 7 方法
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person{
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *personName = (__bridge_transfer NSString *) ABRecordCopyCompositeName(person);
NSLog(@"%@", personName);
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *phoneNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, 0);
//遍历拿到所有的phonenumber,当前取第0个
// for (NSUInteger phoneCounter = 0; phoneCounter < ABMultiValueGetCount(phones); phoneCounter++)
// {
//
// phoneNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, phoneCounter);
//
// NSLog(@"phone : %@",phoneNo);
//
// }
if ([phoneNo hasPrefix:@"+"]) {
phoneNo = [phoneNo substringFromIndex:3];
}
phoneNo = [phoneNo stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@", phoneNo);
if (phone && personName) {
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
return;
}
}
//ios 8 以上
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
long index = ABMultiValueGetIndexForIdentifier(phone, identifier);
NSString *phoneNo = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, index);
NSString *personName = (__bridge_transfer NSString *) ABRecordCopyCompositeName(person);
NSLog(@"%@", personName);
if ([phoneNo hasPrefix:@"+"]) {
phoneNo = [phoneNo substringFromIndex:3];
}
phoneNo = [phoneNo stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@", phoneNo);
if (phone && personName) {
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
return;
}
}
上述方法,直接走第一种代理。
重点来了!
- (void)btnClick{
//联系人选择控制器
ABPeoplePickerNavigationController *nav = [[ABPeoplePickerNavigationController alloc]init];
//设置代理 ABPeoplePickerNavigationControllerDelegate
nav.peoplePickerDelegate =self;
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
}
//弹出视图
[self presentViewController:nav animated:YES completion:nil];
}
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
nav.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
}
加上这一段话后,可实现,在iOS8以上,点击联系人跳转到详情页。
但是还是没有按照需求实现!
解决方案
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
nav.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:@"phoneNumbers.@count == 1"];
}
正确的NSPredicate(谓词)筛选,达到了所需效果。此方法适用iOS8以上。
不详尽的地方请留言指出,谢谢!
网友评论