标签:ios
#pragma mark--选取手机联系人
-(void)selectPeople{
//这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录
int __block tip=0;
//声明一个通讯簿的引用
ABAddressBookRef addBook =nil;
//创建通讯簿的引用
addBook=ABAddressBookCreateWithOptions(NULL, NULL);
//创建一个出事信号量为0的信号
dispatch_semaphore_t sema=dispatch_semaphore_create(0);
//申请访问权限
ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error) {
//greanted为YES是表示用户允许,否则为不允许
if (!greanted) {
tip=1;
}
//发送一次信号
dispatch_semaphore_signal(sema);
});
//等待信号触发
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
if (tip) {
//做一个友好的提示
UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"" message:@"此应用程序没有权限访问您的联系人。您可以在‘设置‘->‘隐私‘->‘通讯录‘->‘健一网‘中启用访问权限" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
[alart show];
}else{
_peoplePicker= [[ABPeoplePickerNavigationController alloc] init];
_peoplePicker.peoplePickerDelegate = self;
NSArray *displayItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil];
_peoplePicker.displayedProperties=displayItems;
// if(IOS8){
// _peoplePicker.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:false];
// }
[self presentViewController:_peoplePicker animated:YES completion:nil];
}
}
#pragma mark -- ABPeoplePickerNavigationControllerDelegate
//ios8执行
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
//查找这条记录中的名字
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
firstName = firstName != nil? firstName:@"";
//查找这条记录中的姓氏
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
lastName = lastName != nil? lastName:@"";
NSLog(@"%@",[NSString stringWithFormat:@"%@%@",firstName,lastName]);
// CFRelease(person);
CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
if(property != kABPersonPhoneProperty) {
return;
}else{
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);
[self dismissViewControllerAnimated:YES completion:^{
selectPeople=[NSString stringWithFormat:@"%@%@",lastName,firstName];
selectNumber=[NSString stringWithFormat:@"%@",(__bridge NSString*)value];
if ([selectNumber isEqualToString:@"(null)"]) {
selectNumber=@"";
}else{
selectNumber=[selectNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
selectNumber=[HYShareObject formatPhoneNum:selectNumber];
}
[self.table reloadData];
}];
}
}
//ios7执行
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_DEPRECATED_IOS(2_0, 8_0){
if(property != kABPersonPhoneProperty) {
return NO;
}else{
ABMultiValueRef valuesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
//查找这条记录中的名字
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
firstName = firstName != nil? firstName:@"";
//查找这条记录中的姓氏
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
lastName = lastName != nil? lastName:@"";
NSLog(@"%@",[NSString stringWithFormat:@"%@%@",firstName,lastName]);
// CFRelease(person);
CFIndex index = ABMultiValueGetIndexForIdentifier(valuesRef,identifier);
CFStringRef value = ABMultiValueCopyValueAtIndex(valuesRef,index);
[self dismissViewControllerAnimated:YES completion:^{
selectPeople=[NSString stringWithFormat:@"%@%@",lastName,firstName];
selectNumber=[NSString stringWithFormat:@"%@",(__bridge NSString*)value];
if ([selectNumber isEqualToString:@"(null)"]) {
selectNumber=@"";
}else{
selectNumber=[selectNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
selectNumber=[HYShareObject formatPhoneNum:selectNumber];
}
[self.table reloadData];
}];
}
return YES;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
标签:ios
原文地址:http://blog.csdn.net/wk598/article/details/46606189